Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

problem with " WaitForSeconds"

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

  1. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    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:
    8,230
    Is the script attached to the GameObject a? Disabling the GameObject will stop the coroutine.
     
  3. rbm123

    rbm123

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