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*; //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.

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*.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?

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()’

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 = 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.