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

Creating a short-living object

Discussion in 'Scripting' started by Boolit, Dec 7, 2014.

  1. Boolit

    Boolit

    Joined:
    Dec 7, 2014
    Posts:
    2
    How do I create an object that should essentially be destroyed a few frames later?

    What I want to do is cast a line from an object to the player, and if it hits something else, create an object that exists one frame, set a few settings (e.g. an audio source), and then delete it again. Think of a sound that plays when hitting an obstacle with the line.

    I managed to get everything working up until the deletion of the object. What I currently do is:

    • Instantiate a new GameObject
    • Set a few things on this GameObject (e.g. add an audio source)
    • Start a coroutine on this GameObject that destroys it after a second
    Is this the optimal way of doing this?

    Ideally, I'd want to create a lot of gameobjects. Like, create a scattering laser effect, with bounces against walls, that is active every frame. Is this possible in Unity? And what is the best way to do this with a script?
     
    Last edited: Dec 7, 2014
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    So, you would like to instantiate an object, have it play a sound, then destroy itself??
    Seems a nasty way to go about building your engine.
     
  3. Boolit

    Boolit

    Joined:
    Dec 7, 2014
    Posts:
    2
    This is just something that I'm trying to create. In the end, I would try to devise a better way. You know what they say about premature optimization...
     
  4. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    Object pooling is more efficient, especially if you plan to create a lot of these game objects. Github has a few repositories with object pooling examples.
     
  5. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    Right, I think I have an idea how:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroySelfTimed : MonoBehaviour
    5. {
    6.     public float lifeTime = 0.01f;     //How many seconds(or fraction thereof) this object will survive
    7.     private bool timeToDie = false;    //The object's trigger of its inevitable DEATH!!!
    8.  
    9.     void Update ()
    10.     {
    11.         lifeTime -= Time.deltaTime;
    12.  
    13.         if (lifeTime <= 0.0f)
    14.         {
    15.             timeToDie = true;
    16.         }
    17.  
    18.         if (timeToDie == true)
    19.         {
    20.             Destroy (gameObject);
    21.         }
    22.     }
    23. }
    Put that script on the object you wish to spawn and quickly kill. You'll have to spawn the object with another script or such though. You can change the amount of time it survives in the inspector.

    EDIT: Wups, I see you already did all that. I should learn to read before I reply -facepalm-
    Regardless I'll leave the script here in case anyone else want's to do something like that.
     
    Last edited: Dec 7, 2014
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It's way easier than that:

    Code (javascript):
    1. var lifetime = 1.0;
    2.  
    3. function Start () {
    4.     Destroy (gameObject, lifetime);
    5. }
    --Eric
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    No I dont.
     
  8. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    Lol I actually just stumbled on that a second ago and came back to tell. Ah well lol
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    renman writes streamlined, highly optimized code first time, everytime

    #prolevel
     
    renman3000 likes this.
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    You know it!
     
    JamesLeeNZ likes this.