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 WILEz1975 · Apr 12, 2016 at 02:43 PM · loadlevelstatic variable

Static variable reset at LoadLevel

I do not understand why my static variables are reset to scene change.

question: If I take a variable on scene A, from an object like this:

StaticData.turretSlot01 = Weapons.FindChild ("_ TurretSlot_3D_01").GetComponent<SS_ShipTurretSlot>();

Where turretSlot01 is obviously static. If there is no Weapons transform in the scene B, the variable is canceled?

Comment
Add comment
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

2 Replies

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

Answer by nonathaj · Apr 12, 2016 at 03:07 PM

Your static variable inherits from UnityEngine.Object and is a part of the scene, thus it is a managed Unity type. These types that are within a scene are destroyed when a new scene is loaded (and Unity actually does the management on the C++ backend and sets references to the C# version of the object as a fake null, which you can read a little bit more about here).

You will notice that the other, non-Unity, types retain their values between scene loads. You can have Unity keep an object alive between scenes by calling DontDestroyOnLoad on the GameObject or the Component itself.

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 WILEz1975 · Apr 12, 2016 at 05:57 PM 0
Share

DontDestroyOnLoad work with components? I can't store the gameobject, I need to use my static variables on other object in the loaded scene. $$anonymous$$new I "DontDestroyOnLoad" is applicable only to gameobjects. How do I maintain a component without a gameobject? And, static variables are not outside of the scene?Indeed iit is not necessary that it is attached in the scene. At least I knew so ;) Thus, only variables/classes of Unity are maintained on loadLevel??

avatar image Bunny83 WILEz1975 · Apr 13, 2016 at 12:19 AM 0
Share

Some facts:

  • Components can't live without a gameobject.

  • Components and GameObjects in the current scene are destroyed when you load a new scene.

  • Yes, static variables are outside of the scene, but not GameObjects and Components, they belong to the scene and are destroyed when you load a new scene. So your static variable still exists but the object it references doesn't.

  • DontDestroyOnLoad can be used on both GameObjects and Components. When used on a component it's the same as using it on the GameObject the component is attached to.

  • DontDestroyOnLoad only works on root GameObjects. So using it on a nested child object has no effect since the object gets wiped out along with the parent.

  • If you only want to keep a child GameObject but not the parent you have to unparent the child first by setting the childs parent property to null and then use DontDestroyOnLoad.

avatar image WILEz1975 · Apr 13, 2016 at 07:51 AM 0
Share

Thanks for the reply . I was trying to store my var with playerpref .

avatar image
0

Answer by aditya · Apr 13, 2016 at 05:49 AM

Create an empty GameObject name it something and assign a Tag to it (must be unique) ... now create a script (for this example i m using C#) and assign it to this GameObject and i prefer to create a prefab of this GameObject so you can just Drag 'n' Drop it in all your scenes .... now in your script copy and paste this

     public static YOUR-CLASS-NAME instance;
 
     void Awake(){
         if (instance == null) {
             DontDestroyOnLoad (gameObject);
             instance = this;
         } else if (instance != this) {
             Destroy (gameObject);
         }
     }

This is actually a singleton design pattern, now make all your static variables a member of this class and access them in any scene without values being changed

Comment
Add comment · 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

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

42 People are following this question.

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

Related Questions

WaitForSeconds load level delay not working 1 Answer

How to have GearVR play smooth during LoadLevelAsync 0 Answers

Scene loads differently when loaded from another scene than selecting that scene first, in the editor. [answered] 2 Answers

Scene switching not working? 2 Answers

3 Questions (performance,static variabales,transfrom) 2 Answers


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