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

Hide Object After Time?

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

  1. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    In my game, I have text that appears (is set active) when a specific object is selected. I want the text to disappear after a few seconds. I was using the Destory.Gameobject function, but the issue is that I want the text to be able to reappear (and redisappear) multiple times, if the user selects it again.

    Is there an elegant way to do this?

    Much appreciated.
     
  2. ldb

    ldb

    Joined:
    Apr 30, 2013
    Posts:
    40
    Could you not just use gameObject.SetActive(true/false); ?
     
  3. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    After a set amount of time though.
     
  4. ldb

    ldb

    Joined:
    Apr 30, 2013
    Posts:
    40
    Ah right, you just use a coroutine. I've just posted a similar example in another thread

    Code (CSharp):
    1.  
    2. void YourFunction()
    3.     {
    4.         StartCoroutine(RemoveAfterSeconds(3, yourGameObject));
    5.     }
    6.  
    7.     IEnumerator RemoveAfterSeconds(int seconds, GameObject obj)
    8.     {
    9.             yield return new WaitForSeconds(seconds);
    10.             obj.SetActive(false);
    11.         }
    12.      
     
    SarahAhmad likes this.
  5. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Thanks, I'll give this a shot! :D
     
  6. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Forgive me, very new to scripting, but I'm getting lots of errors. Ideas?
     
    Last edited: Jan 15, 2015
  7. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Oh! Ok, I kind of got it working here (no errors).

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyObj : MonoBehaviour {
    5.     public GameObject gameObject;
    6.  
    7.     void Start (){
    8.             StartCoroutine(RemoveAfterSeconds(2, gameObject));
    9.     }
    10.     IEnumerator RemoveAfterSeconds (int seconds, GameObject obj){
    11.         yield return new WaitForSeconds(2);
    12.         obj.SetActive(false);
    13.     }
    14.  
    15. }
    16.  
    It works the first time the object is activated, but not if it is activated again. Any ideas why? And how to get the script to run every time the object is set to active?
     
  8. ldb

    ldb

    Joined:
    Apr 30, 2013
    Posts:
    40
    Yes. Starting the Coroutine in the Start function will kick it off when the GameObject is first initialised. You could maybe try changing Start to Awake or OnEnable. Which would restart the coroutine every time the game object is awoken. Or alternatively, put it in a different, public method, which you can then call elsewhere to start the coroutine.

    So for instance, you may have a GameObject called RemoveMe. You would attach your DestroyObj script to your RemoveMe gameObject in the editor. DestroyObj would then look like this:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class DestroyObj : MonoBehaviour {
    5.  
    6.     public void ActivateMe (){
    7.         gameObject.SetActive (true);
    8.     }
    9.  
    10.     public void DeactivateMe (){
    11.         StartCoroutine(RemoveAfterSeconds(2));
    12.     }
    13.  
    14.     IEnumerator RemoveAfterSeconds (int seconds){
    15.         yield return new WaitForSeconds(seconds);
    16.         gameObject.SetActive(false);
    17.     }
    18. }
    19.  
    Note how you do not need to store a reference to gameObject, because MonoBehaviour already has a gameObject reference stored for you.

    Ok... so now, let's say you have Instantiated this RemoveMe Object through code in some other script, like this....

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SomeOtherGameLogicScript : MonoBehaviour {
    5.  
    6.     // You would drag your RemoveMe Object's prefab into this field in the editor
    7.     // So you can create an instance of it in code
    8.     public GameObject My_RemoveMe_Prefab;
    9.  
    10.     // Store reference to the particular instance
    11.     GameObject removeMe;
    12.  
    13.     void Start ()
    14.     {
    15.         removeMe = Instantiate (My_RemoveMe_Prefab, Vector3.zero, Quaternion.identity) as GameObject;
    16.     }
    17.  
    18.     // ... A bunch of game logic could go here.
    19.     // then when you're ready to show/hide your RemoveMe object, you would call this method...
    20.    // Obviously you would only call either DeactivateMe OR ActivateMe below, not both
    21.  
    22.     void someOtherMethod()
    23.     {
    24.         // Hide your removeMe Object
    25.         removeMe.DeactivateMe ();
    26.  
    27.         // Show your removeMe Object
    28.         removeMe.ActivateMe ();
    29.     }
    30. }
    I've had a long day, so I hope this is making sense and you are following me :)
     
    Last edited: Jan 15, 2015
    radler470 likes this.