Home > database >  In C# unity my wait function is not waiting correctly
In C# unity my wait function is not waiting correctly

Time:06-15

I have recently learned coroutines in C# unity and I tried creating a wait function to wait in between codes. It prints both statements at the same time instead of waiting three seconds. Does anyone know a solution to this problem?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Learninghowtoprogram : MonoBehaviour
{
    private void Start()
    {
        print("hello"); 
        wait(3);
        print("hello2");
        
        
    }
    IEnumerator waito(float time)
    {
        yield return new WaitForSeconds(time);
        
    }
    void wait(float time)
    {
        StartCoroutine(waito(time));
        
    }


}

CodePudding user response:

The issue here is when Start() gets called it tries to execute each line one by one

print("hello"); 
wait(3);
print("hello2");

So when you are calling the wait(3) it goes in it's own scope and calls the waito Coroutine.

Now see the yield return new WaitForSeconds(time); is doing it's job correctly that means it's waiting for 3 seconds but in it's scope (inside itself) so what you can do is move print("hello"); and print("hello2"); in the Coroutine itself like this..

public class Learninghowtoprogram : MonoBehaviour
{
    private void Start()
    {
        wait(3);
    }

    void wait(float time)
    {
        StartCoroutine(waito(time));
    }

    IEnumerator waito(float time)
    {
        print("hello");
        yield return new WaitForSeconds(time);
        print("hello2");
    }
}

Or, you can remove wait() and directly start the Coroutine like this also

public class Learninghowtoprogram : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(waito(3));
    }

    IEnumerator waito(float time)
    {
        print("hello");
        yield return new WaitForSeconds(time);
        print("hello2");
    }
}

CodePudding user response:

You can try the following code, hope it will help you.

  using UnityEngine;
  using System.Collections;

 public class ExampleClass : MonoBehaviour
 {
      void Start() 
     {
        print("hello"); 
        StartCoroutine(WaitAndPrint(2.0F));
        print("hello2");
     }
      IEnumerator WaitAndPrint(float waitTime) 
     {
        yield return new WaitForSeconds(waitTime);
     }
 }

The next step is called before the coroutine starts and ends.

  private void Start()
  {
       StartCoroutine("DelayFunc");
  }
   IEnumerator DelayFunc()
  {
         print("hello"); 
         yield return new WaitForSeconds(2);
         print("hello2");    
  }

CodePudding user response:

In the start method, if you are trying to call the void 'wait', don't put a 3 in the parenthesis. You might also want to move the 'Void wait ()' above the 'waitto', like this... `

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Learninghowtoprogram : MonoBehaviour
{
    private void Start()
    {
        print("hello"); 
        wait();
        print("hello2");
        
        
    }
    void wait(float time)
    {
        StartCoroutine(waito(time));
        
    }
    IEnumerator waito(float time)
    {
        yield return new WaitForSeconds(time);
        
    }



}`
  • Related