• 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 Cery · May 08, 2011 at 11:29 AM · variablesingletonreload

Reload a scene and resetting all variables

I want to reset my scene. Nothing new but I want to reset all variables. In the web i found every time the same Application.loadLevel (xx);. But it doesn't reset all variables! One guy sad use singletons but i don't know how they work and what they do. Help please Cery

Ps: sorry for bad english

Comment
Add comment · Show 2
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 Jean-Fabre · May 08, 2011 at 11:38 AM 0
Share

To answer properly, can you clarify what variable or properties exactly are not being reset when loading a new level?

avatar image Justin Warner · May 08, 2011 at 12:49 PM 0
Share

$$anonymous$$ake a functions, that goes to all your other functions, and "resets" them, so a script that controls all the other scripts functions, and change it all that way... Could work, but be time consu$$anonymous$$g.

3 Replies

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

Answer by Peter G · May 08, 2011 at 01:31 PM

Reloading the scene should reset all the variables unless you are using static variables because logically creating a new instance of every object would reset its values to their initial state.

Static variables on the other hand are not destroyed because they are part of a class, not an instance. You have to reset these manually.

DontDestroyOnLoad() is a little different. It tells Unity not to destroy an object when you load a new scene. So these objects won't be reset either because they aren't being destroyed and recreated.

The only way to reset them is just to manually go through and turn the variables back to some initial state. It is your choice how you do that. You can either save all the initial values, or copy the values over from a newly instantiated class.

Comment
Add comment · Show 6 · 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 Cery · May 08, 2011 at 01:53 PM 0
Share

o thanks, $$anonymous$$y Question is answered.

avatar image donskyale_412 · Mar 05, 2012 at 10:29 AM 0
Share

What if you change the static variable by inputting a different value, will the first value be changed to the new one?

avatar image fafase · Mar 05, 2012 at 10:53 AM 0
Share

Static means you create only one instance over the whole game. if you do static var apple:int; apple=8; you change your variable, whether you did it in the orginal script or in an other and anywhere you access that variable it will be 8.

To get the idea, you create an enemy class with a static var enemyCount; to count how many there are, this variable does not belong to each enemy object but to the class Enemy and is the same to all the enemies in the game and when one enemy altered this value it is changed for all of them.

BUT!!! it is not recommended to use static when you simply want to access a value from another script. See a lot of thread about that on this forum. Clarified?

avatar image donskyale_412 · Mar 08, 2012 at 06:57 AM 0
Share

Sorry but I can not find any variable type to use aside from static variable....I tried static variable but nothing happens when I changed the variable through a GUI textfield....

avatar image CowNecromancer · Apr 22, 2015 at 10:46 AM 1
Share

I have a object and when I reset it is still there. There is no scripts on it.

Show more comments
avatar image
0

Answer by IVApps · Oct 28, 2015 at 01:01 PM

You can just create a new script like "lvl01_Updates". Where in the start script you place your variable to 0 or false. You don't interact with the code but its running in the background when you load the scene.

If that answers your question

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
avatar image
1

Answer by YuniorGlez · Aug 20, 2015 at 02:38 PM

I Have the same problem with a part of my code which doesnt execute the next time when I reload the Scene, and when I revise the code I didnt have any static var ( :D ).

But In my case, and I have to tell you due to other have the same case and doesnt have static variables, It was with a variable that when I finish to use it I put it to Mathf.Infinite value, and when the reload scene happens and the Start function tried to make this var again to Time.time ... it looks that it doesnt work :S

So, I rewrite the workflow of the code to make the same things with other strategic and eliminate the Mathf.Infinite.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving last checkpoint hit when reloading scene - Singletons? 1 Answer

Reloading A Gun My Way 2 Answers

creating a simple parameter? 1 Answer

How to subtract variable from singleton instance? 0 Answers

Singleton class variables and inspector 0 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