FaceMann
FaceMann

Reputation: 39

How to make a delay with C# in Unity3D?

I just started to learn c# in unity. I followed a tutorial, but i wanna add some things.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed;
    public Text countText;
    public Text winText;

    private Rigidbody rb;
    private int count;

    void Start ()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        SetCountText ();
        winText.text = "";
    }

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement * speed);
    }

    void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.CompareTag ( "Pick Up"))
        {
            other.gameObject.SetActive (false);
            count = count + 1;
            SetCountText ();
        }
    }

    void SetCountText ()
    {
        countText.text = "Count: " + count.ToString ();
        if (count >= 1)
        {
            winText.text = "You Win!";
            Application.LoadLevel(1);
        }
    }

}

I wanna make a delay between winText.text = "You Win!"; and Application.LoadLevel(1); So you can actually read the text. I hope somebody can help me out!

Upvotes: 0

Views: 5668

Answers (3)

Jerry Switalski
Jerry Switalski

Reputation: 2720

Use Coroutine (as I see this is Unity3D code):

void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.CompareTag ( "Pick Up"))
        {
            other.gameObject.SetActive (false);
            count = count + 1;
            StartCoroutine(SetCountText ());
        }
    }

    IEnumerator SetCountText ()
    {
        countText.text = "Count: " + count.ToString ();
        if (count >= 1)
        {
            winText.text = "You Win!";
            yield return new WaitForSeconds(1f);
            Application.LoadLevel(1);
        }
    }

Upvotes: 5

kfazi
kfazi

Reputation: 617

In Unity waits are usually done with help of a WaitForSeconds class.

In your case you will have to change OnTriggerEnter and SetCountText a bit so that they return IEnumerable type:

IEnumerable OnTriggerEnter(Collider other) 
{
    if (other.gameObject.CompareTag ( "Pick Up"))
    {
        other.gameObject.SetActive (false);
        count = count + 1;
        yield return SetCountText ();
    }
}

IEnumerable SetCountText ()
{
    countText.text = "Count: " + count.ToString ();
    if (count >= 1)
    {
        winText.text = "You Win!";
        yield return new WaitForSeconds(5); // Wait for seconds before changing level
        Application.LoadLevel(1);
    }
}

Upvotes: -1

Yair Nevet
Yair Nevet

Reputation: 13003

I can assume that it's a windows-forms application so that you're better not to use Thread.Sleep as it will block the UI thread.

Instead, use: System.Windows.Forms.Timer

Upvotes: 0

Related Questions