Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GameObject Destroy problem

Discussion in 'Scripting' started by Kevpearman, Apr 4, 2010.

  1. Kevpearman

    Kevpearman

    Joined:
    Dec 9, 2009
    Posts:
    44
    I have a scene with some procedurally generated content and i need to remove and recreate GameObjects at runtime. To do this i have my game class with a transform in it which i am using the designer to associate with a prefab.

    Code (csharp):
    1. Game : MonoBehaviour
    2. {
    3.   Transform myPrefab;
    4. }
    Then in the script i am calling Instantiate() to create a clone of my prefab in the scene, this part works great.

    Code (csharp):
    1. GameObject go = Instantiate(myPrefab, Vector3.Zero, Quaternion.identity) as GameObject;
    When i come to remove the gameobject i assumed i could use

    Code (csharp):
    1. Destroy(go);
    and it would be removed from the scene but it doesn't appear to be removing anything. I can manually delete the cloned objects from the Heirarchy while the game is running and they dissapear fine i just can't seem to remove them inside the C# script


    Any help would be appreciated
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That will work; I guess you have something else going on which is preventing it somehow.

    --Eric
     
  3. Kevpearman

    Kevpearman

    Joined:
    Dec 9, 2009
    Posts:
    44
    I don't know what, it doesn't throw any errors.
    If i destroy the GameObject that is the clone of the prefab i assume it will destroy the mesh etc that is attached to it?
     
  4. Kevpearman

    Kevpearman

    Joined:
    Dec 9, 2009
    Posts:
    44
    Here's some simplified code to show what i'm doing, hopefully something in here will just be wrong :-D

    The transform gridTile gets assigned to a prefab inside the editor. All of the other variables tileSize, gridLocations etc are setup in OnStart or when handling user input.

    I have put print statements into the DestroyGridItems method so i know it's trying to destroy them, they just aren't going anywhere :-(


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System;
    6.  
    7. public partial class Game : MonoBehaviour
    8. {
    9.     public Transform gridTile;
    10.     List<GameObject> gridItems;
    11.     List<Point> gridLocations;
    12.     int tileSize;
    13.  
    14.     private void GenerateGrid(int range)
    15.     {
    16.         //Dispose of any existing grid
    17.         DestroyGridItems();
    18.  
    19.         //get the variables used to calculate the area
    20.         gridItems = new List<GameObject>();
    21.  
    22.         //Instantiate all of the grid tiles
    23.         InstantiateGrid(range, location);
    24.     }
    25.  
    26.     private void InstantiateGrid(int GridRange, Point GridCentre)
    27.     {
    28.         foreach (Point tile in gridLocations)
    29.         {
    30.             gridItems.Add(Instantiate(gridTile, new Vector3(tile.x * tileSize, 0.1f, tile.y * tileSize), Quaternion.identity) as GameObject);
    31.         }
    32.     }
    33.  
    34.     private void DestroyGridItems()
    35.     {
    36.         if (gridItems != null)
    37.         {
    38.             foreach (GameObject obj in gridItems)
    39.             {
    40.                 Destroy(obj);
    41.             }
    42.         }
    43.     }
    44. }
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    gridTile should be a GameObject, not a Transform (also you'll have to remove the prefab from the inspector and replace it before it will change the type properly).

    --Eric
     
  6. Kevpearman

    Kevpearman

    Joined:
    Dec 9, 2009
    Posts:
    44
    I'll double check tomorrow to see if I declared gridTile as a transform or a gameobject, fingers crossed that's all it is. Thanks for your help
     
  7. Kevpearman

    Kevpearman

    Joined:
    Dec 9, 2009
    Posts:
    44
    That sorted it, thanks very much for your help.
    As you said, i did have to go into the editor and reassign my gridTile object.