Froob
Froob

Reputation: 193

How can I wait for 3 seconds and then set a bool to true, in C#?

My script/game/thing make a gameobject move to the right and when I click dance (a button I created) it stops. Then when the counter (I may not need a counter but I want to wait 3 seconds) reaches like 3 (once you click on dance the counter starts) my gameobject is suppose to continue on going to the right.

If you can correct the code that would be cool. If you can correct it and explain to me what i did wrong it would be even more awesome. I just started learning C# on Unity.

using System;
using UnityEngine;
using System.Collections;

public class HeroMouvement : MonoBehaviour
{
    public bool trigger = true;
    public int counter = 0;
    public bool timer = false;

    // Use this for initialization

    void Start()
    {
    }

    // Update is called once per frame

    void Update()
    {  //timer becomes true so i can inc the counter

        if (timer == true)
        {
            counter++;
        }

        if (counter >= 3)
        {
            MoveHero();//goes to the function moveHero
        }

        if (trigger == true)
            transform.Translate(Vector3.right * Time.deltaTime); //This moves the GameObject to the right
    }

    //The button you click to dance 
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 50, 50), "Dance"))
        {
            trigger = false;
            timer = true;//now that the timer is set a true once you click it,The uptade should see that its true and start the counter then the counter once it reaches 3 it goes to the MoveHero function      
        }
    }

    void MoveHero()
    {  //Set the trigger at true so the gameobject can move to the right,the timer is at false and then the counter is reseted at 0.
        trigger = true;
        timer = false;
        counter = 0;
    }
}

Upvotes: 16

Views: 59213

Answers (6)

Jan Thomä
Jan Thomä

Reputation: 13604

You could do it quite easily with coroutines:

void Update()
{
    if (trigger == true)
        transform.Translate(Vector3.right * Time.deltaTime); //This moves the GameObject to the right
}

void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 50, 50), "Dance"))
        {  
           StartCoroutine(DoTheDance());
        }
    }


 public IEnumerator DoTheDance() {
    trigger = false;
    yield return new WaitForSeconds(3f); // waits 3 seconds
    trigger = true; // will make the update method pick up 
 }

See https://docs.unity3d.com/Manual/Coroutines.html for more information about Coroutines and how to use them. They are pretty neat when trying to do a timed series of events.

Upvotes: 15

Alan Sturtivant
Alan Sturtivant

Reputation: 3

I would use this if multi-threading:

    DateTime a = DateTime.Now;
    DateTime b = DateTime.Now.AddSeconds(2);

    while (a < b)
    {
        a = DateTime.Now;
    }

    bool = x;

Upvotes: -3

vicman4
vicman4

Reputation: 191

I think that the easiest way is using Invoke:

Unity3D Invoke

if (timer == true) Invoke("MoveHero", 3);

Upvotes: 10

Basil Mariano
Basil Mariano

Reputation: 2675

I prefer using StartCoroutine here is the link: http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

ex:

   void Foo () { StartCoroutine (Begin ()); }

   IEnumerator Begin ()
    {
        yield return new WaitForSeconds (3);

         // Code here will be executed after 3 secs
        //Do stuff here
    }

Upvotes: 4

VhsPiceros
VhsPiceros

Reputation: 426

if you need only wait, you can use sleep method of Thread

System.Threading.Thread.Sleep(3000);

Upvotes: -8

Andrew Ngo
Andrew Ngo

Reputation: 762

First make counter a float. Then change counter++; to counter += Time.deltaTime. Update() is called for every frame so counter will be 3 on the third frame. Time.deltaTime gives you the time between this frame and the previous frame. Summing it up acts like a timer.

Upvotes: 3

Related Questions