I have a GameObject with associated a javascript. I have defined a private variable like this:

    private var objects = new Array();

now in the Update method I instantiate new objects and push each obtained reference into the private array like this


when a certain event occurs (i.e. the user press a button in the GUI) I'd like all the objects to be destroyed. I am doing that like this

    for (var i =0 ;i<objects.length; i++){

The problem is that although the objects are removed from the array they remain displayed in the Game. What am I doing wrong?


2 Answers 2


My guess is that javascript typing is messing you up.

Where is newObjectInstance created? Are you explicitly declaring its type to be GameObject? (You'd do this by doing var newObjectInstance : GameObject = Instantiate(...); By default the return type of Instantiate is Object and I'm not entirely sure what happens when you try to Destroy it.

Keep in mind that Destroy works on components as well as game objects, so you might be destroying something that's part of the game object but not the game object itself.

Alternatively, try changing your destroy call to Destroy(objects[i].gameObject); to explicitly point to the GameObject itself.

  • \$\begingroup\$ I am not familiar with Javascript so it is indeed possible I am missing something. The object is instantiated as a Rigidbody, or at least I do hope so the code is like newObjectInstance:RigidBody = Instantiate (..); I have also tried: Destroy(objects[i] as Rigidbody) but it still does not work. \$\endgroup\$ May 21, 2012 at 5:05
  • \$\begingroup\$ Well in that case you're just deleting the rigidbody component and not the gameobject itself. \$\endgroup\$
    – Tetrad
    May 21, 2012 at 7:07
  • \$\begingroup\$ should I cast it to a GameObject \$\endgroup\$ May 21, 2012 at 7:45
  • \$\begingroup\$ If you aren't using the list for anything else then change your initial variable to be a gameobject. If you are using it as if it were a rigidbody then a cast won't work since Rigidbody does not derive from GameObject. In that case use the last suggestion in my origional post: access the gameobject using .gameObject. \$\endgroup\$
    – Tetrad
    May 21, 2012 at 11:46
  • \$\begingroup\$ the .gameObject method does not work the compiler says Object does not provide that method. The object I am instantiating is actually a Rigidbody thus if I change the initial variable it says it can't instantiate a rigidbody as a gamobject (that's because as you said there is not any inheritance relationship). So my question is the rigidbody appears on the screen... how can I destroy/remove it? \$\endgroup\$ May 21, 2012 at 18:56

Okay I personally don't know too much about javascript as I personally always code in C# instead. A little check list for you:

  1. Make sure what you push into the array is actually GameObjects and not just components
  2. As Tetrad mentioned, try calling object[i].gameObject, just to make sure it is actual GameObjects you try to destroy
  3. For debugging try to print(object[i]) to see if it is actual GameObjects

(Note: You mispelled --> Destory <--(objects[i]); - Maybe an issue if you copy + pasted your code)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .