- Home /
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);
}
}
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.
Answer by Jan_Sch · Apr 20, 2017 at 11:43 AM
Try to remove the F after the number
Try to start the Coroutine with StartCoroutine("WaitForLoad")
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?
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);
}
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.
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
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.
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
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.
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
Your answer
Follow this Question
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