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
1
Question by BCouto · Oct 27, 2010 at 03:51 AM · scenelevelresetrestartunload

Resetting a Scene

I have a MainMenu, a game screen, and a Win Screen. The game Screen consists of the player, falling enemies, and a countdown timer/score counter.

The game ends when the timer reaches 0:00 and it moves over to the WinScreen. The winScreen has a button to the main menu, but when I click to Play again, it takes me to the game screen which has not been reset. The Score does not start at 0, and the timer keeps on going down into the negatives.

How can I completely reset the scene so that everything starts the way it would when loaded for the first time?

Thanks

EDIT: Everything except for the score and timer seem to be resetting. The timer remains counting even when the scene has been switched.

var startTime : int; var restSeconds : int;

var roundedRestSeconds : int; var displaySeconds : int; var displayMinutes : int;

var countDown : int;

function Update () { //make sure that your time is based on when this script was first called //instead of when your game started startTime = 12;

 var guiTime = Time.time - startTime;

 restSeconds = countDown - (guiTime);

 //display messages or whatever here -->do stuff based on your timer
 if (restSeconds == 60) {
     print ("One Minute Left");
 }
 if (restSeconds == 0) {
     print ("Time is Over");
     MouseFollow.playerScore = 0;
     Application.LoadLevel(2);
 }


 //display the timer
 roundedRestSeconds = Mathf.CeilToInt(restSeconds);
 displaySeconds = roundedRestSeconds % 60;
 displayMinutes = roundedRestSeconds / 60; 

 guiText.text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 
 //GUI.Label (Rect (350, 25, 100, 30), text);

}

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

Answer by · Oct 27, 2010 at 05:30 AM

You could use Application.LoadLevel to reload the level/scene. This would revert all the variables to their default, and seems to be a popular solution (based on answers to similar questions).


Your variables are mostly likely being reset, but the way you're calculating time remaining won't work with the current implementation.

startTime = 12;  // the problem lies here

  • Every frame, you're setting 'startTime' to 12.
  • You're then calculating 'guiTime' by subtracting '12' from Time.time.
  • Time.time is the time in seconds since the start of the game. When you reload the level, the time since the start of the game does not get reset.

You've actually hit the nail on the head with your comment:

    //make sure that your time is based on when this script was first called
    //instead of when your game started

'function Start ()' is called just before any of the Update methods is called the first time. You can store the current Time.time to startTime at this point, and then your calculations should be correct!

function Start ()
{
   startTime = Time.time;
}

You'll also need to remove 'startTime = 12;' from the Update() loop.

Hope this clears things up for you!

Comment
Add comment · Show 7 · 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 BCouto · Oct 27, 2010 at 03:12 PM 0
Share

That's how I load the levels, but when it loads, it's not reset.

avatar image Mike 3 · Oct 27, 2010 at 03:49 PM 2
Share

That would only happen if you have objects marked as DontDestroyOnLoad, or using static variables. Either way, theres no way to reset them automagically - you'll need to destroy them or reset them yourself

avatar image BCouto · Oct 27, 2010 at 04:26 PM 0
Share

The only static variable I have is the player score, I've managed to reset it within another script by changing its value manually. That's all fine now, but the timer still remains counting down into the negatives. I don't have any objects marked as DontDestroyOnLoad.

avatar image · Oct 27, 2010 at 09:51 PM 0
Share

Could you please post the code for your timer in your question so we can take a look at it?

avatar image spilth · Oct 27, 2010 at 10:47 PM 0
Share

It's weird that LoadLevel isn't resetting things but how about just resetting the timer when the level loads?

Show more comments
avatar image
0

Answer by el-santia93 · Aug 28, 2013 at 01:55 PM

had this problem once try this var guiTime = Time.timeSinceLevelLoad - startTime;

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

2 People are following this question.

avatar image avatar image

Related Questions

Application.LoadLevel & Time.timeScale glitch 1 Answer

Reset The Scene ? 2 Answers

I need to restart scene on collision ( when ball hits bottom wall), but this code doesn't work . What should I do ? 1 Answer

How to Reset Scene after Level Completion 1 Answer

1 Scene game|| Reset variables 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