Akari
Akari

Reputation: 856

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;
}

Upvotes: 2

Views: 29417

Answers (3)

Nick
Nick

Reputation: 1035

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.

Upvotes: 12

Agung Pratama
Agung Pratama

Reputation: 3784

Well if you insist want to use Update function, you can. Here is one example how to do it:

private float _elapsedTime = 0;
private int counter = 0;
void Update(){
    if(counter < 7){
        if(_elapsedTime >= 1){
            _elapsedTime = 0; //reset it zero again
            _counter++;
            //instantiate the cube, and update the loading bar here
        }else{
            _elapsedTime += Time.deltaTime;
        }
    }
}

Upvotes: 2

Thomas Ingham
Thomas Ingham

Reputation: 1102

You cannot change the return type of an existing method. Instead you'll want to fire StartCoroutine in your Start or Awake methods and define your IEnumerator as a separate private function of your MonoBehaviour.

Upvotes: 1

Related Questions