Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to wait a few seconds within an If then statement?

Discussion in 'Editor & General Support' started by PrismicStudios, Jun 1, 2014.

  1. PrismicStudios

    PrismicStudios

    Joined:
    Oct 31, 2013
    Posts:
    37
    I've got a game where after you hit a collider 5 times with a projectile, it turns into a black hole. I need to know how to turn off the black hole after 5 seconds. It turns off by resetting the score back to 5.
    How do I do this?


    if (GameManager.BlackHoleScore <= 0)
    { renderer.material.color = Color.black;
    Physics.gravity = new Vector3 (0f, -9.81f, 0f);
    Destroy (col.gameObject);

    //WAIT 5 SECONDS

    GameManager.BlackHoleScore = 5; }
     
  2. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    I think something like this


    Code (csharp):
    1. IEnumerator Blackhole ()
    2.     {
    3.             yield return new WaitForSeconds(5.0f); // waits before continuing in seconds
    4.             // code to do after the wait
    5.     }
    6.  
    and you call that with (put this in the if statement)
    Code (csharp):
    1. StartCoroutine(Blackhole());
     
  3. PrismicStudios

    PrismicStudios

    Joined:
    Oct 31, 2013
    Posts:
    37
    So it should be something like this? It doesn't wait, it just resets immediately. What am I doing wrong?

    using UnityEngine;
    using System.Collections;

    public class GravityShift : MonoBehaviour {

    public static Vector3 BlackHolePosition;
    public static Quaternion BlackHoleRotation;

    void Update () {

    }

    IEnumerator Blackhole ()

    {

    yield return new WaitForSeconds(5.0f); // waits before continuing in seconds

    // code to do after the wait

    }

    void OnCollisionEnter (Collision col)
    {
    if ((GameManager.MyGrav == 1 || GameManager.MyGrav == 0 ) GameManager.BlackHoleScore >= 1 col.gameObject.tag == "Player1Ball") {
    Physics.gravity = new Vector3 (0f, -9.81f, 10f);
    GameManager.MyGrav = 2;
    renderer.material.color = Color.blue;
    GameManager.Player1Balls += 5;
    GameManager.BlackHoleScore -= 1;
    }

    if ((GameManager.MyGrav == 2 || GameManager.MyGrav == 0 ) GameManager.BlackHoleScore >= 1 col.gameObject.tag == "Player2Ball")
    { Physics.gravity = new Vector3 (0f, -9.81f, -10f);
    renderer.material.color = Color.red;
    GameManager.MyGrav = 1;
    GameManager.Player2Balls += 5;
    GameManager.BlackHoleScore -= 1;
    }

    if (GameManager.BlackHoleScore <= 0)
    { renderer.material.color = Color.black;
    Physics.gravity = new Vector3 (0f, -9.81f, 0f);
    Destroy (col.gameObject);
    StartCoroutine(Blackhole());
    GameManager.BlackHoleScore = 5;
    renderer.material.color = Color.white;}
    }
    }
     
  4. Robotic_Soul

    Robotic_Soul

    Joined:
    Mar 29, 2016
    Posts:
    3
    Good answer! Minor edit for clarity:

     
    farahnajwa30 and aigamecreator like this.