Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Destroying objects after 3 seconds

Discussion in 'Editor & General Support' started by satanicrohan, Aug 21, 2011.

  1. satanicrohan

    satanicrohan

    Joined:
    Mar 31, 2011
    Posts:
    4
    Hi everybody,
    I am creating a game in which I want to display an object after 3 seconds and destroy it after the next 3 second, create and display another object after next 3 seconds. Can somebody help me with this?
    I am using Time.time but the object keep appearing faster and faster
    Regards
    satanicrohan
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
  3. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
  4. lukasaurus

    lukasaurus

    Joined:
    Aug 21, 2011
    Posts:
    360
    If you are doing the Walkerboys tutorial, it really is best if you figure this out yourself. Everything you need to know was taught to do in the video tutorials, and you need to find a way to apply them to the sphere.

    Here is a tip though. Don't destroy the object. Hide it, and change it's position before enabling it's render again. And use the InvokeRepeating to countdown.

    I added a boolean variable to my enemyScript that the designer can check if the object is a sphere, and then conditional statements within the code to make the object flash for 3 seconds if it is a sphere.

    It took me ages to figure it out and I tried many different ways, and even emailed the walkerboys for help (haven't responded yet) before approaching it from another angle and succeeded.

    If you aren't doing the walkerboys tutorial, then I suppose none of the above will make any sense. But it certainly sounds like you are :)

    My Lab 1 project is finished (at least in terms of requirements). I'm just modifying it and using it to learn the basics before I submit it and get the password for Lab 2.
     
  5. a1s2d3f4

    a1s2d3f4

    Joined:
    Mar 20, 2011
    Posts:
    415
    You could also do this:

    Code (csharp):
    1.  
    2. var WaitTime : float = 3;
    3.  
    4. function Update () {
    5.      
    6.        if (something) {
    7.           destroyFunction();
    8.           }
    9. }
    10.  
    11. function destroyFunction () {
    12.  
    13.        yield WaitForSeconds (WaitTime);
    14.        Destroy (gameObject);
    15.        //Anything else you might want to do herre
    16. }
    17.  
    By doing it this way, you have the option to do more than just destroy the object.
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    While we're throwing in ideas, you can also do this
    Code (csharp):
    1.  
    2. var lifeTime : float=3.0f;
    3.  
    4. function Start () {
    5.     Invoke ("Death", lifeTime);
    6. }
    7.  
    8. function Death () {
    9.     Destroy (gameObject);
    10.     //Anything else you might want to do here
    11. }
    12.  

    As for your actual question, it would look a bit more like this
    Code (csharp):
    1.  
    2. var prefabObj : GameObject;
    3. private var obj : GameObject;
    4.  
    5. function Start () {
    6.     InvokeRepeating ("Switcher", 3, 3);
    7. }
    8.  
    9. function Switcher () {
    10.     if (obj == null) {
    11.         obj = Instantiate (prefabObj);
    12.     } else {
    13.         Destroy (obj);
    14.     }
    15. }
    16.  
     
  7. satanicrohan

    satanicrohan

    Joined:
    Mar 31, 2011
    Posts:
    4
    Thank you guys for helping me out...i will keep u updated after i try out these options
    but currently i m using Time.deltaTime and it works fine