x


Destroy instances from an array

Hi, i'm new to the forums and Unity/JS. I've been banging my head on this for 2 days now. i've tried so many different solutions and have looked at every relevant post online.

The worst part is that i swear there is a very simple solution to this.

I've got a grid of instantiated objects (currently all from a single prefab) which i'm adding to an array by mouse clicking the object. Once i reach the desired selection count I'd like to Destroy those instances and remove them from the array.

Here's the relevant script:

          for(var i : int =0; i < selected.length; i ++)
          {
              //set the current selected object to "thisObject"
              var thisObject = selected[i]; //this was used by a different failed attempt to Destroy the instances...


              //used for debuging, change the material to indicate the object is un-selected
              //thisObject.transform.renderer.material.color = Color.white;  


              //find the CleanUp script and assign it to the variable "otherScript"
              var otherScript : CleanUp;
              otherScript = GetComponent("CleanUp");

              //script name. function name(function parameter)
          otherScript.CleanThis(true, thisObject);
          //remove the current object from the array
          selected.RemoveAt(i);   
          //add score
          score++;

          }
         }
    }
    }

The above script is on an empty object in my scene. On my prefab object there is the "CleanUp" script which looks like this:

//static var alive : boolean = true;
//var destroyTime : int;
//var emitParticles : Transform;

function CleanThis(clean : boolean, theObject)
{
    if(clean == true){
    Destroy(theObject);
    //Instantiate(emitParticles, transform.position, Quaternion.identity);
    //Destroy(theObject, destroyTime);
}
}

Any help would be greatly appreciated. If you can spell it out, so i can learn from it/my mistakes, that would be even better!

Thanks.

more ▼

asked May 26 '11 at 02:33 AM

Drexster gravatar image

Drexster
31 6 8 10

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

I think your problem is doing the RemoveAt(i) from inside the loop. RemoveAt will remove the item from the array and shift everything after it down by one. This means that what was at index 4 now is at index 3. Well, if i was 3, then at the end of the loop it increments to 4, the value at 4 is now the value that was at 5 on the last iteration. Effectively you will be removing every other item until you reach the half-way point, and then you will be indexing off the end of the array (which causes an error).

The fix is to just wait until after the loop and do 'selected.Clear()'

more ▼

answered May 26 '11 at 06:33 AM

hellcats gravatar image

hellcats
651 8 12 23

Wicked that worked!

Thank you!

May 26 '11 at 06:46 AM Drexster
(comments are locked)
10|3000 characters needed characters left

I've gotten a bit closer after a break, some food and fresh air. I now have 2/3 of my objects destroying, only the first and third objects from the array are destroying. I'm assuming it's something to do with one of my for loops?

here are the iterated scripts:

if(selected.length == minSelection)
    {
       var isTheSame : boolean = false;
       for(var y : int = 0; y < selected.length; y ++)
       {
         if(selected[0].GetComponent("ObjectClass").objectType != selected[y].GetComponent("ObjectClass").objectType)
          {
          isTheSame = false;
          Debug.Log("You do not have the same selected");
          }

         else
         {
          isTheSame = true;
          Debug.Log("You have the same selected!");          
          for(var i : int =0; i < selected.length; i ++)
          {
              var thisObject : GameObject = selected[i].gameObject;
              var otherScript = thisObject.GetComponent(CleanUp);
              otherScript.CleanThis(true, thisObject);
              selected.RemoveAt(i);
              score++;
              if (i == selected.length)
              {break;}
          }
         }
       }
    }

and the CleanUp script:

var destroyTime : int;
var emitParticles : Transform;

function CleanThis(clean : boolean, theObject : GameObject)
{
    if(clean == true)
    {
       Destroy(theObject.gameObject, destroyTime);
       Instantiate(emitParticles, theObject.transform.position, Quaternion.identity);
    }
}

Getting closer. Any thoughts/comments?

more ▼

answered May 26 '11 at 06:32 AM

Drexster gravatar image

Drexster
31 6 8 10

(comments are locked)
10|3000 characters needed characters left

function Update () { for(var i : int = 0 ; i < 5 ; i++) { var position : Vector3 = Vector3(Random.Range(-10.0,10.0),Random.Range(-10.0,10.0),40.0); go[i] = Instantiate(prefab,position,Quaternion.identity); } for(var j : int = 0 ; j < 5 ; j++) { Destroy(go[j]); } whats wrong with this code? Please help me out ..novice.

more ▼

answered Dec 29 '12 at 09:02 PM

GTMJP13 gravatar image

GTMJP13
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x764
x204

asked: May 26 '11 at 02:33 AM

Seen: 1410 times

Last Updated: Dec 29 '12 at 09:02 PM