Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by sacredgeometry · Sep 13, 2011 at 06:23 PM · c#gameobjectstatic

Static gameObject?

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Sep 13, 2011 at 09:09 PM 0
Share

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;

?

avatar image sacredgeometry · Sep 13, 2011 at 09:34 PM 0
Share

I meant is the first synonymous with the second examples.

avatar image Waz · Sep 13, 2011 at 10:15 PM 0
Share

So not the checkbox that Eric5h5 is talking about.

avatar image sacredgeometry · Sep 13, 2011 at 10:21 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sacredgeometry · Sep 13, 2011 at 06:52 PM 0
Share

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

avatar image Eric5h5 · Aug 26, 2013 at 03:34 PM 0
Share

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.

avatar image Fattie · Aug 26, 2013 at 05:18 PM 0
Share

great to know! nice improvement from Unity.

avatar image
24

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Aug 26, 2013 at 11:26 AM 0
Share

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

avatar image peterpunk · Feb 03, 2016 at 09:02 AM 0
Share

For static variables and their relationship with gameObject instances I wrote some notes here: http://designagame.eu/2016/01/untangling-gameobject-state-in-unity/

avatar image Abhiroop-Tandon · Sep 17, 2016 at 01:12 PM 0
Share

@eldeorn Will a ground be considered a static object ?? if so then basically usually the player and enemies aren't static within a game ??

avatar image Bunny83 Abhiroop-Tandon · Sep 17, 2016 at 05:37 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sacredgeometry · Sep 13, 2011 at 10:24 PM 0
Share

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.

avatar image Waz · Sep 14, 2011 at 12:10 PM 1
Share

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.

avatar image sacredgeometry · Sep 14, 2011 at 04:58 PM 0
Share

Where instance is what? A GameObject variable?

avatar image Fattie · Aug 26, 2013 at 11:30 AM 3
Share

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

static classes are used everywhere and always.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Attaching GameObject and ParticleSystem to C# script 1 Answer

GameObjects static array NullReferenceException 1 Answer

Multiple Cars not working 1 Answer

When is a static game object static? 1 Answer

Displaying a static variable from another script with OnGUI 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges