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. Dismiss Notice

If Statement + Coroutine?

Discussion in 'Scripting' started by radler470, Jan 15, 2015.

  1. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    I am having an issue. I would like a GameObject to disappear after a few seconds after it is activated in my scene. I am able to do that with the script below, but I also want it to be able to work multiple times. Right now it works, but if the object is set to Active a second time in the scene, it does not disappear. I was thinking I could use an if statement to declare "if (gameObject.activeInHierarchy == true), but I am unable to get the actual code block to work properly.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyObj : MonoBehaviour {
    5.     public GameObject gameObject;
    6.  
    7.     void Start (){
    8. // If statement somewhere in here?
    9.             StartCoroutine(RemoveAfterSeconds(2, gameObject));
    10.        
    11.     }
    12.     IEnumerator RemoveAfterSeconds (int seconds, GameObject obj){
    13.         yield return new WaitForSeconds(2);
    14.         obj.SetActive(false);
    15.     }
    16.  
    17. }
    18.  
    19.  
    20.  
    Any help with this issue would be greatly appreciated. I am also very new to Unity and C#, so if you could be as specific as possible, I would appreciate that as well.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Put the StartCoroutine call in OnEnable instead of Start
     
  3. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Holy crap, that was disgustingly simple. Thank you!

    Updated code for anyone stumbling across this thread:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyObj : MonoBehaviour {
    5.     public GameObject gameObject;
    6.  
    7.     void OnEnable (){
    8.         if (gameObject.activeInHierarchy == true)          
    9.             StartCoroutine(RemoveAfterSeconds(2, gameObject));
    10.        
    11.     }
    12.     IEnumerator RemoveAfterSeconds (int seconds, GameObject obj){
    13.         yield return new WaitForSeconds(2);
    14.         obj.SetActive(false);
    15.     }
    16.  
    17. }
     
  4. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Hmmm... Looks like it's working ok in the editor, but not when I build. Any ideas why this might be?
     
    Last edited: Jan 15, 2015
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Your if statement is unnecessary. Because it's being called in OnEnable you can assume that it's active (otherwise OnEnable wouldn't fire in the first place).

    You also don't need to pass gameObject into the Coroutine. Just use gameObject.SetActive(false);

    Edit: I see what you're doing. OnEnable is when the GameObject with *that* script is enabled. Looks like you're trying to assign some other GameObject to it, which won't work.
     
  6. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    I see, thanks for explaining. I'm still fumbling around with C# a lot, so sometimes I make poor desicions. Can you show me an example of the code to illustrate your point? I'm not sure how to call the GameObject that has the script applied to it.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Attach your script to the thing you want to turn itself off.

    Code (csharp):
    1.  
    2. void OnEnable()
    3. {
    4.     StartCoroutine(TurnOff());
    5. }
    6.  
    7. IEnumerator TurnOff()
    8. {
    9.     yield return new WaitForSeconds(2);
    10.     gameObject.SetActive(false);
    11. }
    12.  
    Or if you wanted to do it via Update for some reason

    Code (csharp):
    1.  
    2. float startTime;
    3.  
    4. void OnEnable()
    5. {
    6.     startTime = Time.time;
    7. }
    8.  
    9. void Update()
    10. {
    11.     if (Time.time - startTime > 2)
    12.         gameObject.SetActive(false);
    13. }
    14.  
     
    radler470 likes this.
  8. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Ah, ok, I see now. Thank you! For some reason it's still only working in the editor, and not when I build. Do you know why that might be?
     
  9. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Not working on a build is a problem not related to the script. It can be hard to find. If I remember right when it happened to me, it was because I had a javascript and a c# script with the same name. It's pretty vague and it might not even have been that. Look for any warnings in the console and probably do a search.
     
    radler470 likes this.
  10. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Having bigger issues now... I installed the new patch and now I'm getting an error that I can't seem to get rid of. Even tried reverting back to an older patch. Uninstalling the player, reinstalling from an older version and the error still won't go away. I hope I'm not screwed here. :confused::(:eek:

     
  11. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    I was able to get it working, thank you. :)
     
  12. vinit_ingle

    vinit_ingle

    Joined:
    Jun 20, 2013
    Posts:
    2
    I'm stuck with the same issue....my code works in editor but not in build....How did u solve the issue???
     
  13. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    Using Invoke may be easier for you:

    Code (CSharp):
    1. Invoke("DisableThis",2);
    2.  
    3. void DisableThis()
    4. {
    5.      gameObject.SetActive(false);
    6. }