- Home /
Detect at runtime if a given GameObject is static
How to detect at runtime if a given GameObject is static or statically batched? i.e. whether I will be able to move and rotate that at runtime together with mesh renderer.
Answer by tomekkie2 · Aug 19, 2013 at 02:24 PM
I have found my own answer in the meantime which is:
renderers[0].isPartOfStaticBatch
at runtime, inside the game, is equivalent to
isStatic
in Editor, where
Renderer[] renderers = GetComponentsInChildren<Renderer>();
which now tells me: if false means I will be able to rotate these objects in game.
This is great man, thanks! Unity should really warn about isStatic being editor only... shit compiles and runs fine in release builds, but silently returns unexpected values.
Probably want to use GetComponentsInChildren<Renderer>(includeInactive: true)
so if they're hidden (and presumably unhidden during gameplay) you still check them. (If they're hidden, you probably shouldn't enable static batching.)
Answer by Jamora · Aug 19, 2013 at 01:37 PM
The staticicity of an object is an editor only attribute, so it is not possible to access it during runtime (as far as I know). If you need to know it in-game, you will have to store the data in a class and access it during the game.
The simplest, yet maybe not the most effective, (or OOP for that matter) solution would be to have a script something like this attached to each object.
[ExecuteInEditMode]
public class Staticiticy{
public bool staticObject;
#if UNITY_EDITOR
void Update(){
staticObject = gameObject.isStatic;
}
#endif
}
You can then get the component and check if staticobject is true or false.