0

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!

Jerry Switalski
  • 2,690
  • 1
  • 18
  • 36
FaceMann
  • 39
  • 1
  • 11
  • 1
    Related: http://stackoverflow.com/q/5449956/3273247 – nulltron Jan 20 '16 at 16:05
  • That should help you – nulltron Jan 20 '16 at 16:05
  • That gives me a error: Assets/Scripts/PlayerController.cs(48,25): error CS0103: The name `Thread' does not exist in the current context – FaceMann Jan 20 '16 at 16:06
  • Try System.Threading.Thread.Sleep(50); – blas3nik Jan 20 '16 at 16:10
  • 1
    @FaceMann : that's because you would need to include the right namespace for it to compile. But calling `Thread.Sleep` is an awful code smell. And in your case it would block the UI thread, something you definitely don't want. – Falanwe Jan 20 '16 at 16:10
  • When i put System.Threading.Thread.Sleep(5000); between winText.text and application.LoadLevel it pauzes for a second but the text you win isn't visible – FaceMann Jan 20 '16 at 16:15
  • 2
    @FaceMann : never use `Thread.Sleep`. Ever. Wipe this method from your memory and you'll become a better programmer. – Falanwe Jan 20 '16 at 16:17
  • `Thread.Sleep` is sometimes usefull with testing the appliaction behaviour – Jerry Switalski Jan 20 '16 at 16:22
  • @JerrySwitalski Indeed, it is often useful for testing purpose, mostly to simulate a heavy load on a thread that makes it completely irresponsive (not something you should aim for). But it makes it to production quite often and is nearly always the symptom of something very wrong in the app. – Falanwe Jan 20 '16 at 17:07
  • How silly. You can't use Thread,Sleep in Unity, or any frame-based system. **Of course, JUST USE INVOKE is what the OP is looking for.** – Fattie Jan 21 '16 at 12:46

3 Answers3

5

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);
        }
    }
Jerry Switalski
  • 2,690
  • 1
  • 18
  • 36
0

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

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
  • 2
    This is a Unity application, so the windows form timer is not really an option, but you're right that `Thread.Sleep` is a very bad idea indeed. – Falanwe Jan 20 '16 at 16:14
-1

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);
    }
}
kfazi
  • 617
  • 1
  • 9
  • 21
  • As a beginner, the OP should just use `Invoke`. This is a billion-times duplicate - nobody should have answered it. This site is turning in to trash because people keep answering the lowest-quality Unity questions. – Fattie Jan 21 '16 at 03:16