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

Creating new GameObject after destroying

Discussion in '2D' started by adnan_gamedev, Jan 31, 2015.

  1. adnan_gamedev

    adnan_gamedev

    Joined:
    Jan 31, 2015
    Posts:
    1
    Hi, As a beginner I'm trying to make a space shooter game. Currently the problem I'm facing is if "Spaceship" gameobject collide with "EnemyShip" gameobject 3 times after that spaceship isn't moving from it's place. How can I solve it.

    Code (JavaScript):
    1. public var bullet : GameObject;
    2. public var spaceship : GameObject;
    3.  
    4. function Start () {
    5.      
    6. }
    7.  
    8. function Update () {
    9.     rigidbody2D.velocity.x = Input.GetAxis("Horizontal")*10;
    10.     rigidbody2D.velocity.y = Input.GetAxis("Vertical")*10;
    11.    
    12.     //When spacebar is pressed
    13.     if(Input.GetKeyDown("space")){
    14.         Instantiate(bullet, transform.position, Quaternion.identity);
    15.     }
    16. }
    17. // Function called when the enemy collides with another object
    18. function OnTriggerEnter2D(obj : Collider2D) {
    19.     var name = obj.gameObject.name;
    20.     // If it collided with the spaceship
    21.     if (name == "enemy(Clone)") {
    22.         Destroy(gameObject);
    23.         Destroy(obj.gameObject);
    24.         Debug.Log("HIT");
    25.         createSpaceShip();
    26.     }
    27. }
    28. function createSpaceShip(){
    29.  
    30.     var point = new Vector2(0,-4);
    31.     Instantiate(spaceship, point, Quaternion.identity);
    32. }
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Not sure if that is your problem, but I think you shouldn't issue a Destroy(gameObject) until you are completely finished with the gameObject...
     
  3. lupo

    lupo

    Joined:
    Apr 4, 2009
    Posts:
    43
    Hi Adnan,
    I just worked out a fairly simple enemy instantiate and destroy solution, take a look and see if it is any help. It uses a random enemy instantiation (which you could simplify if you have only one type of entity) as well as a bullet detection script that destroys the enemy and then calls the instantiation script again. There is always one enemy.

    As PGJ says, you don't want to destroy any enemies that have scripts or the script will be destroyed, so I put my instantiation script on an empty game object.

    http://www.3dcognition.com/unity-random-enemy-instantiation/

    I also have an example where you can destroy an object with four hits:

    http://www.3dcognition.com/unity-destroy-objects-four-hits-show-gui-score/
     
    theANMATOR2b likes this.
  4. DustyMcp

    DustyMcp

    Joined:
    May 23, 2013
    Posts:
    25
    Your problem is you instantiate your object on a position you just erased, try instantiating the new object on the old position before you delete the gameobject holding the position.
     
  5. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    5,984
    Just a suggestion, it is easier on performance if you instantiate a bunch of enemies at the beginning and then deactivate them with GameObject.SetActive(false) when they die. That way you can reuse them without Unity having to recreate them from scratch.