Search code examples
unity-game-engineunityscript

how to wait for a specific number of seconds in unity


I want to make a loading bar in unity 2d game by instantiating 7 cubes every 1 second. I used : yield WaitForSeconds(1); in the function update after every instantiate statement but it didn't work :(( I got an error which is :

Script error : Update() can not be a coroutine.

Any other idea?

I made a new scene and named it "lose" then I wrote this script and attached it to the main camera:

#pragma strict

//var loadingBar: Transform;
var loading_bar : GameObject;

function Update()
{
    Instantiate(loadingBar,Vector3(-1.849,-2.9371,2),Quaternion.identity);

    gameTimer();


    Instantiate(loadingBar,Vector3(-1.2909,-2.937,2),Quaternion.identity);

    gameTimer();

    Instantiate(loadingBar,Vector3(-0.5566,-2.93711,2),Quaternion.identity);

    gameTimer();

    Instantiate(loadingBar,Vector3(0.148236,-2.93711,2),Quaternion.identity);

    gameTimer();

    Instantiate(loadingBar,Vector3(0.823772,-2.93711,2),Quaternion.identity);

    gameTimer();

    Instantiate(loadingBar,Vector3(1.440567,-2.93711,2),Quaternion.identity);

    gameTimer();

    Instantiate(loadingBar,Vector3(2.057361,-2.93711,2),Quaternion.identity);

    loadingTimer();

    Application.LoadLevel(1);
}


function OnGUI()
{
    GUI.color = Color.green;
    GUI.Label(Rect(400,350,500,500),"<color=green><size=100>Lose</size></color>");
}

function loadingTimer()
{
    yield WaitForSeconds(1);
}

enter image description here

I want to these cubes to appear after each other by 1 second so it will seem like a loading bar ...

I solved this problem by this way ::

#pragma strict

var loadingBar: Transform;
var finished : boolean = false;

function Update()
{
    loadingTimer();

    if (finished == true)
    {
        Application.LoadLevel(1);
        finished= false;
    }
}


function OnGUI()
{
    GUI.color = Color.green;
    GUI.Label(Rect(295,320,500,500),"<color=green><size=100>Lose</size></color>");

}


function loadingTimer()
{
    Instantiate(loadingBar,Vector3(-1.9,-2.9371,2),Quaternion.identity);
    yield WaitForSeconds(0.28);
    Instantiate(loadingBar,Vector3(-1.3,-2.937,2),Quaternion.identity);
    yield WaitForSeconds(0.28);
    Instantiate(loadingBar,Vector3(-1.3,-2.937,2),Quaternion.identity);
    yield WaitForSeconds(0.28);
    Instantiate(loadingBar,Vector3(-0.7,-2.93711,2),Quaternion.identity);
    yield WaitForSeconds(0.28);
    Instantiate(loadingBar,Vector3(-0.1,-2.93711,2),Quaternion.identity);
    yield WaitForSeconds(0.28);
    Instantiate(loadingBar,Vector3(0.5,-2.93711,2),Quaternion.identity);
    yield WaitForSeconds(0.28);
    Instantiate(loadingBar,Vector3(1.1,-2.93711,2),Quaternion.identity);
    yield WaitForSeconds(0.28);
    Instantiate(loadingBar,Vector3(1.7,-2.93711,2),Quaternion.identity);

    finished= true;
}

Solution

  • First of all, You cant use yield WaitForSeconds in Update function. You need to intoduce IEnumator. In your case I can say the following code may help you.

    public class Loader : MonoBehaviour 
    {
        public GameObject cube;
        private bool finished = false;
        private Vector3[] positions = new Vector3[7] {new Vector3(-1.849,-2.9371,2), new Vector3(-1.2909,-2.937,2), new Vector3(-0.5566,-2.93711,2),new Vector3(0.148236,-2.93711,2),new Vector3(0.823772,-2.93711,2),new Vector3(1.440567,-2.93711,2),new Vector3(2.057361,-2.93711,2)};
        private int loaderCounter=0;
    
        void Start () 
        {
            StartCoroutine(StartLoader());
        }
    
        IEnumerator StartLoader () 
        {
            Instantiate(cube,positions[loaderCounter],Quaternion.identity);
            yield return new WaitForSeconds(1);
            loaderCounter++;
            if(loaderCounter==7)
            {
                finished=true;
            }
            if(!finished)
            {
                StartCoroutine(StartLoader());
            }
            else
            {
                Application.LoadLevel(1);
            }
        }
    }
    

    Let me know if there is any problem after this. Just use javascript syntax of variable declarations.