Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Talantyyr · Aug 15, 2013 at 08:15 PM · sceneresetreloadapplication.loadlevel

Reset a scene

I'm currently working on a card game. When all cards are cleared, or the player finishes the game, i'm enabling a button to go back to the main menu or restart the level via:

 Application.LoadLevel ("MainMenu");

or

 Application.LoadLevel (Application.loadedLevel);

When i reload the level again, nothing is resettet. The time / score / cards... everything is still there.

What i've read, Application.LoadLevel should reset everything, unless i declare something as static or use "DontDestroyOnLoad" in the Awake function. (Which i've done nowhere in the entire scene)

Another funny thing is, that when the scene is reloaded, i get tons of

 UnassignedReferenceException: The variable movesGUI of 'cardSelection' has not been     assigned.
 You probably need to assign the movesGUI variable of the cardSelection script in the inspector.

errors...

I've declared "movesGUI" as public and dragged a GameObject with a GuiText on it. I'm also doing this with 3 other GUIText elements (they don't throw any errors on reload...)

Do you have any idea, why my scene isn't completley reset?

Is there any way to force reset a scene?

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
2
Best Answer

Answer by Xtro · Aug 15, 2013 at 08:24 PM

You do correct to reset the level.

I think the problem is about the location of the data(time, score,cards,...)

Where and How do you store those values ? In static variables ? In a ScriptableObject as a prefab??

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 Talantyyr · Aug 15, 2013 at 08:40 PM 0
Share

No, i'm sure i'm not using static on any variables / classes / etc...

Time is handled like this:

     public float timeLeft = 180.0f;
     public float time = 180.0f;
 
  .... 
 
     void Update () {
         
        timeLeft = time - Time.time;
        timeGUI.text = "" + (int)timeLeft;
 
 ...
 }

As you can see i'm initializing the time / timeLeft variable, so it should be resetted to that value?

I've rechecked my game... I've called the "GameWon()" function in the beginning for debug purposes, so i have missed that most of the reloading works, sry for that.

The Cards are correctly replaced, and shuffled again. The Score is reset, but the time is not.

And i still get that awkward error which i posted above.

The game also doesn't respond anymore, but this could be due to the unassigned reference error....

Edit:

I've done further research... I've removed the movesGUI variable from my script and tried again. The error is gone, but so is the gameObject holding the scripts to generate the Cards and to sleect them...) I wonder why the cards are generated and placed anyway....

I'm not using GameObject.Destroy anywhere... really strange...

avatar image Joyrider · Aug 15, 2013 at 08:42 PM 0
Share

You should initialize in the Start function

avatar image Talantyyr · Aug 15, 2013 at 08:48 PM 0
Share

That doesn't work either, Joyrider

avatar image save · Aug 15, 2013 at 08:58 PM 1
Share

Time.time is the start since the game initialized / simulation started, not the start from the last scene load. When I calculate time I simply do this:

 timeVariable += 1*Time.deltaTime;

It's frame independent, it never fails, and is easy to read when functions gets nasty.

Another thing you can do is to set time to Time.time+180.0f when level loads, in Start or Awake preferably.

 void Start () {
     time = Time.time+180.0f;
 }

Hope it works out!

avatar image Xtro · Aug 15, 2013 at 09:00 PM 0
Share

Are you using an 3rd party addon something like "Persist Playmode" ??

That kind of addons helps you to modify the scene objects in playmode persistently. If you have that addon it could save the inspector values with the runtime values. And they can be reloaded with the saved value on restart.

Show more comments
avatar image
2

Answer by stoward94 · Mar 18, 2014 at 10:27 PM

I was having a very similar problem with a scene I was working on. It was a simply race course in which allowed you to replay the track at the end. I was using Time.time in order to calculate and store the total time for the race along with other things. However when I reloaded the scene the Time never reset hence would never enter in to given condition for if statements and so on.

What fixed this for me was the following:

  void Start() {
        totalTime = Time.timeSinceLevelLoad;    
     }

Instead of

 totalTime = Time.time;

This wouldn't technically reset the time, but just use the time since the level reloaded or simply started.

Hope this helps :)

Comment
Add comment · Show 1 · 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 BadSeedProductions · Nov 05, 2015 at 05:35 AM 0
Share

This was my problem! Thanks

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

19 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

Related Questions

When ai touches gameobject reset scene. The code does not work any suggestions 2 Answers

GameManager and scene design issue. 1 Answer

My button won't load a scene 1 Answer

How to prevent Static List from resetting when i run the game in the editor? 1 Answer

Timer not working as expected. 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