Search Unity

Destroy gameobject not working

Discussion in 'Scripting' started by PelleFixar, Dec 11, 2018.

Thread Status:
Not open for further replies.
  1. PelleFixar

    PelleFixar

    Joined:
    Nov 20, 2018
    Posts:
    6
    I'm trying to create a game like Banished or Castles and Kingdoms, and I'm having some trouble with the farm. I made 4 stages of the farm; Empty, Early, Late & Harvestable.

    I made a simple script that destroys the earlier stage and replaces it with the next stage by pressing J, K and L (I'm going to have a timer do that automatically later, just have this for testing right now)

    Code (CSharp):
    1. public class Instantiate : MonoBehaviour {
    2.  
    3.     public GameObject gameobj1;
    4.     public GameObject gameobj2;
    5.  
    6.     public GameObject gameobj3;
    7.     public GameObject gameobj4;
    8.  
    9.     public Transform farmSpawn;
    10.  
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.         if (Input.GetKeyDown(KeyCode.J))
    22.         {
    23.             Destroy(gameobj1);
    24.             Instantiate(gameobj2, farmSpawn.position, farmSpawn.rotation);
    25.         }
    26.  
    27.         if (Input.GetKeyDown(KeyCode.K))
    28.         {
    29.             Destroy(gameobj2);
    30.             Instantiate(gameobj3, farmSpawn.position, farmSpawn.rotation);
    31.         }
    32.  
    33.         if (Input.GetKeyDown(KeyCode.L))
    34.         {
    35.             Destroy(gameobj3);
    36.             Instantiate(gameobj4, farmSpawn.position, farmSpawn.rotation);
    37.         }
    38.     }
    39. }
    40.  
    So I placed a prefab of an empty farm on the map and used that as gameobj1, then I used prefabs that I hadn't placed on the map as gameobj 2, 3 & 4, and then placed the script on a GameController. When I press J, the empty farm gets destroyed and is replaced by the early farm, but when I try pressing K and L, the earlier stages don't get destroyed.


    pls help
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    by the looks of it you are attempting to destroy the prefab, not the instance of the prefab in the scene.

    You can get a reference to the instance in the scene that you create by using syntax like

    Code (csharp):
    1.  
    2. GameObject instance = Instantiate(prefab, ....
    3.  
     
  3. shawndingo

    shawndingo

    Joined:
    Oct 4, 2018
    Posts:
    83
    As said above, you need to store a separate GameObject as a clone and then destroy it.

    Like this

    Code (CSharp):
    1. GameObject go = Instantiate(gameobj2, farmSpawn.position, farmSpawn.rotation);
    2.  
    3. // And then when you want to destroy the clone call..
    4.  
    5. Destroy(go);
     
    PelleFixar likes this.
  4. PelleFixar

    PelleFixar

    Joined:
    Nov 20, 2018
    Posts:
    6
    Thank you both for your help!
     
  5. aaayushanand5

    aaayushanand5

    Joined:
    Jul 4, 2021
    Posts:
    1
    How do I get the references to objects which are pre-instantiated by me manually in the scene?
     
  6. SeerSucker69

    SeerSucker69

    Joined:
    Mar 6, 2021
    Posts:
    68
    You declare a public game object variable to store it
    Then drag and drop the gameobject from the Hierarchy to the inspector.

    Tutorial:

     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Please don't necro/hijack threads. Create your own.
     
Thread Status:
Not open for further replies.