• 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
0
Question by Ruckrova · Dec 23, 2014 at 12:06 AM · script error

count down and restart level not working ?

this script below works to count down if the application.loadLevel(0); is not included, it would seem that the "if" statement is not skipping the loadlevel , when I include it, it reload straight away and the counter start at the set time again any ideas why ? .

pragma strict

var timer : float = 60.0f;

function FixedUpdate() { timer -= Time.fixedDeltaTime;

 if (timer <= 0)
 {    
     timer = 0;
     Application.LoadLevel(0);
 }
 

}

function OnGUI() { GUI.Box(new Rect(10, 10, 50, 20), "" + timer.ToString("0")); }

Comment
Add comment · Show 1
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 Unitraxx · Dec 23, 2014 at 01:15 AM 0
Share

Some questions: if it reloads straight away, does it keep doing that? or does it only load it once and then start working properly? Also, if you remove the loadlevel, does the timer ever display a number lower than 0 or does it correctly stop at 0?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MrTiger · Dec 23, 2014 at 03:20 AM

if you want to load level every 60 sec use this simple script

    float timer = 60;
         void FixedUpdate () {
             timer -= 1 * Time.deltaTime ;
             print (timer);
             if (timer <= 0)
             {
                 Application.LoadLevel(0);
             }
         }

this is a C# script i hope you can understand

and if you can't load the level then check did you add the scene in build setting and can you see "0" in the right side ,if not save the scene then add it to the build setting

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
0

Answer by Ruckrova · Dec 23, 2014 at 06:23 AM

thanks Mr tiger

I want the game to count down from 120 sec and if the player cant complete the task in time the level restarts or even better they get the option to restart or quit .

It's "a run against the clock" , in my script " timer" is a public float and I can reset the timer so i can work out how long it should take while Im doing it , before I build the level.

I would rather Java as I don't understand the differences between C+ and java and all the other scripts are in java

I have the scene in the build window with the 0 as you describe.

I have a quit script if they do finish the task now but will need to work out how to stop the clock when they complete the task .

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 MrTiger · Dec 23, 2014 at 07:37 AM 0
Share

Hmmm , Normally when you reload a level everything in that level will be recreated unless if you use "DontDestroyOnLoad(this);" your code should work fine .. Anyhow "Time.timeScale = 0;" will freezes the app if you want to know more about this click here but you need to change "Time.timeScale = 1;" before you restart or on Awake/Start function to unfreeze the game/app if you need help in java i can try to post the code for you

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

27 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

Related Questions

'parent' is not a member of 'Object'. 1 Answer

Csharp script parsing error 2 Answers

my STFPSC script has errors can anyone help me? 0 Answers

Copy script not working 1 Answer

AddComponent() is adding 64 instances of my script to my GO 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