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
0
Question by maria · May 01, 2012 at 03:34 AM · loadleveltimers

Timers, LoadLevel

Hi Im trying to load another level after 60 seconds into the game. Im new to unity heres my code so far. Do I need a update() method maybe??

 private float countdownTimerStartTime=0;
 private int endtime =5;
 
 void OnAwake(){
     // record current time for timer to count down from
     countdownTimerStartTime = Time.time;
     if(countdownTimerStartTime == endtime)
     {
         Application.LoadLevel(3);
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Seth-Bergman · May 01, 2012 at 03:43 AM

edit: sorry, you're using c# .. anyway, you could do:

 void Awake()
 {
     StartCoroutine(MyLoadLevel(5.0f));
 }
 
 IEnumerator MyLoadLevel(float delay)
 {
     yield return new WaitForSeconds(delay);
     Application.LoadLevel(3);
 }
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 Bunny83 · May 01, 2012 at 03:51 AM

Ok first you should take a deep look into the scripting reference and maybe read some beginner-tutorials. The Overridable Functions should give you an idea.

Some points about your code:

  • There's no "OnAwake", it's called just "Awake"

  • Awake and Start are just being called once. To check a progressing value (Time.time) you have to check it every frame (in Update) or use a coroutine which kind of runs parallel to your other stuff.

  • The time returned by Time.time is a float (floating point number). It holds numbers with a fractional part. Checking float values for equality doesn't work most the time since 5.000011 is not the same as 5.000012

  • Your endtime value is an integer value (whole number without fractional part). Mixing floats with ints is also not a good idea. The time is a float, so any other time value should also be a float.

  • Instead of == you should use >=

If you really want to just load a new level after a fix time without any further condition you could use a coroutine which waits the desired amount of time.

The Start() function can also be a coroutine so you can just do something like this:

 private float endtime = 60.0f;  // 60 seconds
 
 IEnumerator Start()
 {
     yield return new WaitForSeconds(endtime);
     Application.LoadLevel(3);
 }

Comment
Add comment · Show 2 · 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 Noah-1 · May 01, 2012 at 04:01 AM 0
Share

Sorry, I miss read your question, the best way to solve this is with WaitForSeconds like Bunny83 says.

avatar image maria · May 02, 2012 at 09:26 PM 0
Share

Thank you all for your comments and help much appreciated. Thats great help worked for me perfect and I learned what I was doing wrong thank you guys :))

avatar image
1

Answer by -hiTo- · May 01, 2012 at 04:18 AM

Awake() only fires once, so in order to determine if 60 seconds has gone by, you need to either check all the time, if the time has gone by, in an Update, for example. Or start a timer that will execute commands after a specified time. Coroutines are good, but if you're new, I would suggest working with Invokes instead. They come more natural.

 private void Awake()
 {
     Invoke("StartMyLevel", endTime);
 }
 private void StartMyLevel()
 {
     Application.LoadLevel("YourLevel");
 }
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

8 People are following this question.

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

Related Questions

Delays when move to another scene 1 Answer

Next lvl code help 1 Answer

level load problem 2 Answers

Giant spike with level load causes crash with iphone 1 Answer

Creating a GameObject fails directly after Loading a Level 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