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

Instantiate gameObject after 1st gameObject is destroyed?

Discussion in 'Scripting' started by pixel_ninja, Dec 9, 2010.

  1. pixel_ninja

    pixel_ninja

    Joined:
    Oct 6, 2010
    Posts:
    11
    I have a script written,
    where my player will destroy an enemy and cause it to disappear. But i'm trying to instantiate another
    game object afterwards. Does anyone have suggestions?
    This is what I have so far:


    The spirit variable above is the sphere that i'm trying to instantiate.

    On a side note, this is the health script I have attached to the enemy. I'm hoping that I'm at least using the right logic about where I need to place the instantiate?
     
    Last edited: Dec 9, 2010
  2. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    Not sure why you're creating the spirit G/O when it collides with things by using OnTriggerEnter?

    But anyways, this is what you need.
    Code (csharp):
    1.  
    2. //Also up top, var spirit needs to be a GameObject not a transform.
    3.  
    4. var spirit : GameObject;
    5.  
    6. /.....
    7.  
    8. //Change your current if statement block to
    9. if( hitPoints < 1 ) { //Also I would change this to, hitPoints <=0, so it's only when he is truly dead.
    10. var spirit = Instantiate(spirit, gameObject.transform.position, Quaternion.identity);
    11. Destroy(gameObject, 0.0);
    12. }
     
  3. pixel_ninja

    pixel_ninja

    Joined:
    Oct 6, 2010
    Posts:
    11
    My bad. I'm trying to beat programming into my head as much as I possibly can since i'm so new to this.
    And you, my friend. Have been a huge help.
     
  4. Animus

    Animus

    Joined:
    Jun 11, 2010
    Posts:
    4
    *deleted*
     
  5. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    No worries :). We all go through that initial learning process, just glad I could help! :D