Microsoft.Unity.Analyzers icon indicating copy to clipboard operation
Microsoft.Unity.Analyzers copied to clipboard

Lint-ban GameObject.isStatic in non-editor code

Open jeffomatic opened this issue 3 years ago • 0 comments

Problem statement

When read in the editor, including during playmode, the isStaticproperty of a GameObject instance may be true or false depending on how the object is configured in the inspector. However, in builds, the property is always false. At least, I have observed it to be always false in Unity 2021.3f1.

The fact that runtime behavior is different between builds and the editor playmode can lead to subtle bugs, including the one I just spent the whole day debugging! It is fair to say that neither the property name nor the docs make this behavior discoverable. Honestly, I would prefer if Unity just crashed if the property were read from a build runtime, but a lint ban would do nicely.

There seems to be a decade's worth of grousing about this issue in the forums.

Proposed solution

Issue a warning any time isStatic is read from a GameObject instance, except when any of the following conditions are true:

jeffomatic avatar Jul 10 '22 02:07 jeffomatic