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

Setting a GameObject to Be Active or Inactive Based on Elapsed Time

Discussion in 'Scripting' started by tmartinez88, Sep 29, 2015.

  1. tmartinez88

    tmartinez88

    Joined:
    Jul 24, 2015
    Posts:
    6
    Hey all,

    Thanks in advance for your help. I've been looking at a bunch of video tutorials here and on youtube and I can't seem to piece together something which seems like it would be very easy.

    I want to count how many seconds have elapsed since the game started (I think using Time.time?) and if a certain number of seconds pass to render a specific gameobject I have the script attached to (it happens to be a plane with a movie texture, although I tried with just a primitive cube). This is what I have so far... Probly looks dummbbb but what am I doing wrong?

    Maybe I need to turn the number from Time.time into a variable somehow and check that against an if statement?

    Any resources or help appreciated!


    using UnityEngine;
    using System.Collections;

    public class timer2 : MonoBehaviour {

    // Use this for initialization
    void Start () {
    gameObject.SetActive (false);
    }

    void Update (){
    if (Time.time >= 10) {
    gameObject.SetActive (true);
    }
    }
    }
     
  2. tmartinez88

    tmartinez88

    Joined:
    Jul 24, 2015
    Posts:
    6
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class timer2 : MonoBehaviour {
    5.    
    6.     public float count = 0;
    7.     // Use this for initialization
    8.     void Start () {
    9.         gameObject.SetActive (false);
    10.     }
    11.  
    12.     void Update (){
    13.         if (Time.time >= 10) {
    14.             gameObject.SetActive (true);
    15.         }
    16.         }
    17. }
    18.  
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well, that's the same issue that someone had in another thread.

    If you attach this script to an object, the object will set itself to be inactive. The script will stop, too.
    Attach this script to anther object, make a public variable of type GameObject and assign the object you want to become active to the slot in the inspector.

    It's also a nice situation to use the Invoke method in Start in order to achieve that effect. This way, you get rid of the code in Update which would otherwise (unnecessarily) set the object to be active in every frame as soon as Time.time hits/passes 10 seconds.
     
    Kiwasi likes this.
  4. tmartinez88

    tmartinez88

    Joined:
    Jul 24, 2015
    Posts:
    6
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class enumerator : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start ()
    8.     {
    9.         StartCoroutine (movie1 ());
    10.     }
    11.    
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.  
    18.     IEnumerator movie1()
    19.     {
    20.         yield return new WaitForSeconds (10);
    21.  
    22.         Renderer r = GetComponent<Renderer>();
    23.         MovieTexture movie = (MovieTexture)r.material.mainTexture;
    24.         movie.Play();
    25.  
    26.         yield return new WaitForSeconds (12); //video is x seconds long
    27.         Destroy (gameObject);
    28.     }
    29.  
    30. }
    31.  
    Hey Thanks for your help. That took me on a path that got me a lot closer - though using coroutines. I'm almost there, I just need the mesh renderer to be disabled when the game is started, then to be re-enabled. The tutorial vid on this site doesn't quite work.


    The attached script will play a movie texture though for a certain amount of time then destroy the game object just like I want.

    Any tips for having the meshrenderer disabled on default, then re-enabled after the 10 second wait time???
     
  5. tmartinez88

    tmartinez88

    Joined:
    Jul 24, 2015
    Posts:
    6
    Hey y'all I did it. The following C# script will hide a 3d object with a video texture until 10 seconds pass - then render the object - play the video for 12 seconds then destroy the game object. I have this specific game object (a plane) parented to my first person camera, so that as I am playing the game a plane pops up like a hud and a video plays and follows my head position as I am playing. You can modify the seconds of course to have a video pop in after any number of seconds and play for any number of seconds before it gets DESTROYYYEEDDDDDD.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class event1 : MonoBehaviour {
    5.     public Renderer rend;
    6.     // Use this for initialization
    7.     void Start ()
    8.     {
    9.         rend = GetComponent<Renderer> ();
    10.         rend.enabled = false;
    11.         StartCoroutine (movie1 ());
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.      
    17.     }
    18.  
    19.     IEnumerator movie1()
    20.     {
    21.         yield return new WaitForSeconds (10);
    22.         rend = GetComponent<Renderer> ();
    23.         rend.enabled = true;
    24.         Renderer r = GetComponent<Renderer>();
    25.         MovieTexture movie = (MovieTexture)r.material.mainTexture;
    26.         movie.Play();
    27.      
    28.         yield return new WaitForSeconds (12); //video is x seconds long
    29.         Destroy (gameObject);
    30.     }
    31.  
    32. }
     
    thomas_python, sallyk and Suddoha like this.
  6. tmartinez88

    tmartinez88

    Joined:
    Jul 24, 2015
    Posts:
    6
    Hope this helps somebody!
     
    thomas_python likes this.