Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SirSmileX · Apr 19, 2017 at 06:36 PM · strange ioc

WaitForSeconds doesn't work in Coroutine.

Hey Guys, I am having some problems lately with coroutines. Somehow the code executes the LoadScene right after the if-statement equals true and I don't know why. It should wait 10 seconds. The canPass from GameManager is a boolean.

 public class Finish : MonoBehaviour {
 
     public Camera cam;
     public GameObject player;
     public ParticleSystem particle;
 
     private Animator anim;
 
     void Start ()
     {
         anim = GetComponent<Animator> ();
     }
 
     void Update()
     {
         if (cam.GetComponent<GameManager> ().canPass)
             anim.SetTrigger ("activate");
     }
 
     void OnCollisionEnter()
     {
         if (gameObject.name == "playerCube" && cam.GetComponent<GameManager> ().canPass)
         {
             StartCoroutine (WaitForLoad ());
         }
     }
 
     IEnumerator WaitForLoad()
     {
         yield return new WaitForSeconds (10f);
         SceneManager.LoadScene (cam.GetComponent<GameManager> ().nextScene);
     }
 }
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 Bryangs · Apr 19, 2017 at 09:26 PM 0
Share

Try using Debug.Log before and after the WaitForSeconds line to see if it is really skipping it, or try to remove the F after the number.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jan_Sch · Apr 20, 2017 at 11:43 AM

  1. Try to remove the F after the number

  2. Try to start the Coroutine with StartCoroutine("WaitForLoad")

  3. If this doesn't solve your problem, use Debug.Log() before and after the WaitForSeconds() and in the if-statement.

EDIT: I tried your script and it works perfectly for me. Maybe there is another script that is accidentally loading the scene?

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 pranavgadamsetty · Apr 20, 2017 at 11:51 AM 0
Share

@Jan_Sch,

The following script is what I use and this works perfectly.

 public GameObject scanObject;
     public GameObject scanObject2;
     Vector3 originalScanPosition;
     Quaternion originalScanOrientation;
     private void Start()
     {
         
     }
 
     public void OnScanClick()
     {
         StartCoroutine(DelayObjActive());        
     }
 
     IEnumerator DelayObjActive()
     {
         yield return new WaitForSecondsRealtime(2);
         scanObject.SetActive(true);        
     }
 
avatar image TotalForce · Dec 20, 2020 at 07:42 PM 0
Share

I used Coroutines and yield return new WaitForSeconds() before and this all worked very well whatever I did before. In my new script nothing worked, no errors were shown whatever I did but Unity refused to WaitForSeconds(). I have changed it to WaitForSecondsRealtime and it has began to work! What the heck this is all about, accidental regular bug? Documentation says nothing. I'm using Unity v19.

Edit: I've found real answer in my own answer after several $$anonymous$$s I have written it x) WaitForSecs() always uses Time.timeScale, so if timeScale is 0 then WaitForSecs() don't work. WaitForSecsRealtime() was added in Unity5.4 exactly for that reason.

avatar image
0

Answer by pranavgadamsetty · Apr 20, 2017 at 07:26 AM

Hi,

I think what you want is WaitForSecondsRealtime(). Check out the links below for more description. As the name suggests, this function waits for the amount of time specified in real-time.

https://docs.unity3d.com/ScriptReference/WaitForSeconds.html

https://docs.unity3d.com/ScriptReference/WaitForSecondsRealtime.html

If you want an example to see how this command is implemented, check out the below link

http://answers.unity3d.com/questions/301868/yield-waitforseconds-outside-of-timescale.html

Hope this helps, Pranav

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 aklgupta · Apr 20, 2017 at 08:44 AM

The yield return new WaitForSeconds (10f); should work. I believe that the problem lies somewhere else. You should instead check if the coroutine WaitForLoad() is being started at the correct instant. You could try using Debug.log in the if block as well as a Debug.log in the beginning of WaitForLoad().

Also, for something like this, you don't really need a coroutine, you could instead use Invoke(). It is simpler and easier.

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 pranavgadamsetty · Apr 20, 2017 at 09:37 AM 0
Share

@aklgupta,

I never actually understood how WaitForSeconds() works so I always used WaitForSecondsRealtime(). Can you explain what the result of WaitForSeconds(10f) would be in terms of "TimeScale" mentioned in the Unity Scripting page

Thanks, Pranav

avatar image aklgupta pranavgadamsetty · Apr 21, 2017 at 05:34 AM 0
Share

I am a novice in Unity, so I don't know the exact working of difference, however, I believe this is what you want to know:

WaitForSeconds is effected by the time scale ie if your time scale is 0.5, then WaitForSeconds(5) will end up waiting for 10 seconds, where as WaitForSecondsRealtime is uneffected by the time scale, and hence WaitForSecondsRealtime(5) will always wait for 5 secs, regardless of the time scale.

avatar image pranavgadamsetty aklgupta · Apr 21, 2017 at 09:18 AM 0
Share

@aklgupta,

That's exactly what i needed to know. I just couldn't wrap my head around how the time scale works and how it affects the function WaitForSeconds(x). Appreciate the early reply

Thanks, Pranav

Show more comments

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

10 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

Related Questions

My Build Platform won't change. 0 Answers

Spherical panorama texture with transparent sky 0 Answers

How would I make a character select screen? 0 Answers

Need help with script 1 Answer

How to include js libraries ,.css 0 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