- Home /
Static gameObject?
What is a static gameObject? Is it similar to a static class? If so then how do you access/reference it?
Do you mean a GameObject for which the "static" checkbox is set in the Inspector, or do you mean code like:
static GameObject foo;
or
static var foo : GameObject;
?
I meant is the first synonymous with the second examples.
huh? Yes both, I was asking if the checkboxes did what you listed ..ie made the gameobject static :P
Answer by Eric5h5 · Sep 13, 2011 at 06:35 PM
You mark a game object static if you want it to use static batching, or if you want it to be included when lightmapping.
Ahh that makes sense, so is the best way to reference an object statically to have a static reference?
Note that there is no longer such a thing as just a "static GameObject". If you click on the arrow next to the "static" checkbox, you see that there are various properties such as lightmap, occluder, batching, etc. So you could have an object marked as static for lightmapping, but not be a part of static batching, and various other combos.
Answer by eldeorn · Aug 26, 2013 at 11:25 AM
You are misunderstanding the difference between static objects in the scene and static variables in scripts / classes.
Note that the same word .. "static" .. happens to be used in English for these two utterly unrelated concepts. To repeat, there is no relationship, whatsoever, between these two things - it just happens to be the same word.
A static object in the scene is for example a rock, a wall or a tree that you want to have lightmaps on, recieve shadows, use as navigation obstacles and so on.
Again, this is simply using the word "static" as in English .. ie, it means nothing more than "not moving," you could equally just say "stationary" or "not moving" or "never moves." So, a car or a dragon is "not static" whereas a house or a statue is "static."
Whereas...
A "static variable" in programming is a particular technique available in most programming languages. Essentially, using a "static variable" in a script means (in short) that no matter from what script object, you always acess the same memory. If you have a static variable in a class, it doesnt belong to a single object of that class - it's global for all instances of the class. (Fully understanding "static variables" in programming is a large topic you could study for some time on.)
An excellent explanation of this common misunderstanding due to the word being the same!!
For static variables and their relationship with gameObject instances I wrote some notes here: http://designagame.eu/2016/01/untangling-gameobject-state-in-unity/
@eldeorn Will a ground be considered a static object ?? if so then basically usually the player and enemies aren't static within a game ??
Nothing is "considered" static in Unity. You can mark it as static by checking the static checkbox of the gameobject. Static gameobjects must never be moved rotated or scaled during runtime. Objects marked as static might undergo certain optimisations especially when it comes to batching and collision detection.
As soon as an object might move / rotate / scale or change it's appearence it should not be marked as static.
As another general rule: Every object that moves / rotates should have at least a kinematic rigidbody in order to properly detect collisions. However to detect collisions at least one of the two objects has to have a non-kinematic rigidbody.
Answer by Waz · Sep 13, 2011 at 10:19 PM
A static variable, whether a GameObject (which is an object reference) or any other type, is one that is global and singular to the application, scoped to the class in which it is declared. There is no such thing as a "static class".
O$$anonymous$$ so I guess I should rephrase my second question. What is the best way to reference a gameobject if its the only instance.
Foo.instance
is a common pattern (and set instance=this in Foo.Awake). There is no best way, it depends on circumstances. $$anonymous$$aybe for example you have a ubiquitous base class where you could have a foo variable.
@waz - why do you say there is no such thing as a static class?
static classes are used everywhere and always.