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 Steven-1 · Dec 29, 2010 at 12:23 PM · timeyieldwaitforsecondsdelay

How to delay a function (how to use WaitForSeconds() and yield)

I know this has been asked before, but it just doesn't work, and I don't see what I'm dooing wrong.

(I use javascript)

function Quit (score:int) //quits to main menu (scene) { if (currentLevel>0) scores[currentLevel-1] = Mathf.Max(scores[currentLevel-1],score); currentLevel=0; //Application.LoadLevel(currentLevel); LoadCurrentLevel(); }

function LoadCurrentLevel() //is een coroutine (blijkbaar automatisch ofzo doordat er yield instaat ? { yield WaitForSeconds (2); Application.LoadLevel(currentLevel); }

I've read the documentation and I thought this would work, but it seems to wait for ever (instead of 2 seconds), Application.LoadLevel(currentLevel) is never executed.

don't know if it could be relevant, but this code is not part of a script attached to a gameobject (and it's a singleton).

EDIT:

I tried it differently now, like this:

function LoadCurrentLevel(wait:boolean) { //if (wait) yield WaitForSeconds (2.5); //Application.LoadLevel(currentLevel); if (wait) { isWaiting=true; waitingStart=Time.time; } else Application.LoadLevel(currentLevel); }

 function Update()
 {
     if (isWaiting)
     {
         if (Time.time>waitingStart+2.5)
         {
             isWaiting=false;
             Application.LoadLevel(currentLevel);
         }
     }
 }

But it still doesn't work, it does exactly the same. why is that ?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Meltdown · Dec 29, 2010 at 12:28 PM

Your script is correct. You need to attach your script to a GameObject.

What I do is create an Empty GameObject for all my games, and call it 'GameManager'.

Any game-wide scripts are then attached to that game object.

Comment
Add comment · Show 5 · 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 Steven-1 · Dec 29, 2010 at 12:39 PM 0
Share

is there a way without attaching it to a GameObject ? it's just a lot cleaner without

(also, I would have to rewrite all the singleton stuff, so it works with the gameobject and it doesn't get deleted when switching levels ...)

avatar image Steven-1 · Dec 29, 2010 at 12:57 PM 0
Share

tried a different approach, see edit

avatar image Meltdown · Dec 29, 2010 at 01:08 PM 0
Share

As far as I know it needs to be attached to a GameObject. There is nothing messy about having a GameObject to manage all your global game logic. I find it very clean and efficient.

avatar image Steven-1 · Dec 29, 2010 at 01:15 PM 0
Share

ok but I still don't get at all why my 2nd approach doesn't work. (the messy thing is that I have to create a gameobject at the beginning of the game and make sure it never gets deleted and is accessable from everywhere)

avatar image Bryan 4 · Apr 27, 2011 at 05:25 PM 0
Share

DontDestroyOnLoad(this.gameObject); will make any object a script is attached to be a perm. resident of your game world even on scene changes. The easiest approach is to create one at the beginning map, tag it with a unique tag to retrieve the object easily and then use its script.

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

No one has followed this question yet.

Related Questions

Yield only working once? 0 Answers

Loop Animation with delay-variable 1 Answer

Make delay for spawn 3 Answers

How to Delay A Ball shooting script 1 Answer

Loading Screen and Lerp 3 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