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;
static var foo : GameObject;
I meant is the first synonymous with the second examples.
So not the checkbox that Eric5h5 is talking about.
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.
great to know! nice improvement from Unity.
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."
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".
OK 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. Maybe for example you have a ubiquitous base class where you could have a foo variable.
Where instance is what? A GameObject variable?
@waz - why do you say there is no such thing as a static class?
static classes are used everywhere and always.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
8 People are following this question.
Displaying a static variable from another script with OnGUI
Distribute terrain in zones
When is a static game object static?
Attaching GameObject and ParticleSystem to C# script
Multiple Cars not working