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

problem with " WaitForSeconds"

Discussion in 'Editor & General Support' started by rbm123, Jun 5, 2017.

  1. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    129
    I write this code:

    Code (CSharp):
    1. public class Itempick: MonoBehaviour
    2.  
    3. {
    4.  
    5.   public GameObject a;
    6.  
    7.  
    8.  
    9.   void Start()
    10.  
    11.   {
    12.  
    13.    
    14.  
    15.   }
    16.  
    17.  
    18.  
    19.   void OnTriggerEnter(Collider other)
    20.  
    21.   {
    22.  
    23.     if(other.CompareTag("Player"))
    24.  
    25.     {
    26.  
    27.       StartCoroutine (wait ());
    28.  
    29.     }
    30.  
    31. }
    32.  
    33.  
    34.  
    35. IEnumerator wait(){
    36.  
    37. a.gameObject.SetActive (true);
    38.  
    39.       yield return new WaitForSeconds(3) ;
    40.  
    41.       a.gameObject.SetActive (false);
    42.  
    43. }
    44.  
    45.  
    46.  
    47. }
    (its exactly the same as example in unity document)!

    But only "a.gameObject.SetActive (true);" works and the other one not!
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    Is the script attached to the GameObject a? Disabling the GameObject will stop the coroutine.
     
  3. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    129
    NO - this script is attached to other GameObject (Coin) and in Inspector I choose one GameObject (Text) which I want to active and detective.