x


Static gameObject?

What is a static gameObject? Is it similar to a static class? If so then how do you access/reference it?

more ▼

asked Sep 13, 2011 at 06:23 PM

sacredgeometry gravatar image

sacredgeometry
381 32 32 47

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;

?

Sep 13, 2011 at 09:09 PM Waz

I meant is the first synonymous with the second examples.

Sep 13, 2011 at 09:34 PM sacredgeometry

So not the checkbox that Eric5h5 is talking about.

Sep 13, 2011 at 10:15 PM Waz

huh? Yes both, I was asking if the checkboxes did what you listed ..ie made the gameobject static :P

Sep 13, 2011 at 10:21 PM sacredgeometry
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You mark a game object static if you want it to use static batching, or if you want it to be included when lightmapping.

more ▼

answered Sep 13, 2011 at 06:35 PM

Eric5h5 gravatar image

Eric5h5
107k 54 180 650

Ahh that makes sense, so is the best way to reference an object statically to have a static reference?

Sep 13, 2011 at 06:52 PM sacredgeometry

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.

Aug 26, 2013 at 03:34 PM Eric5h5

great to know! nice improvement from Unity.

Aug 26, 2013 at 05:18 PM Fattie
(comments are locked)
10|3000 characters needed characters left

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.)

more ▼

answered Aug 26, 2013 at 11:25 AM

eldeorn gravatar image

eldeorn
76 1 7

An excellent explanation of this common misunderstanding due to the word being the same!!

Aug 26, 2013 at 11:26 AM Fattie
(comments are locked)
10|3000 characters needed characters left

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".

more ▼

answered Sep 13, 2011 at 10:19 PM

Waz gravatar image

Waz
7.2k 66 63 104

OK so I guess I should rephrase my second question. What is the best way to reference a gameobject if its the only instance.

Sep 13, 2011 at 10:24 PM sacredgeometry

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.

Sep 14, 2011 at 12:10 PM Waz

Where instance is what? A GameObject variable?

Sep 14, 2011 at 04:58 PM sacredgeometry

@waz - why do you say there is no such thing as a static class?

static classes are used everywhere and always.

Aug 26, 2013 at 11:30 AM Fattie
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9324
x3428
x426

asked: Sep 13, 2011 at 06:23 PM

Seen: 10822 times

Last Updated: Aug 26, 2013 at 05:18 PM