Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

    objects.push(newObjectInstance);

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++){
        Destory(objects[i]);
    }
    objects.clear();

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
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. –  www.Sillitoy.com May 21 '12 at 5:05
    
Well in that case you're just deleting the rigidbody component and not the gameobject itself. –  Tetrad May 21 '12 at 7:07
    
should I cast it to a GameObject –  www.Sillitoy.com May 21 '12 at 7:45
    
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. –  Tetrad May 21 '12 at 11:46
    
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? –  www.Sillitoy.com May 21 '12 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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.