Retrieve multiple items from an array of GameObjects

Hello,

I’m quite new to JavaScript. I’ve managed to create a 100 cube array and disable them one at a time but I would like to be able to retrieve more than one of those cubes (a variable amount) in that array and disable them at the same time.

var goPrefab : GameObject;
var xEdge = 5;  // width
var yEdge = 20;  // height
var zEdge = 1;  // depth
var v3Center = Vector3.zero;


private var arCube : GameObject[,,] = new GameObject[xEdge, yEdge, zEdge];
 
function Start() {
    for (var i = 0; i < xEdge; i++) {
       for (var j = 0; j < yEdge; j++) {
         for (var k = 0; k < zEdge; k++) {
          var x = v3Center.x + xEdge - i - 0;
          var y = v3Center.y + yEdge - j - 1;
          var z = v3Center.z + zEdge - k - 1;
          arCube[i,j,k] = Instantiate(goPrefab, Vector3(x,y,z), Quaternion.identity);
         }
       }
    }
}

 
function Update()
{

if (Input.GetMouseButtonDown(0)) {
		
		var Cube : GameObject = arCube[Random.Range(0, xEdge), Random.Range(0, yEdge), Random.Range(0, zEdge)];
		
		if (Cube.renderer.enabled) {
		Cube.renderer.enabled = false;
	  	} 	
	  	else { Update(); }
	  	
	}
}

Could it be possible, for example, to repeat the last part an X number of times?

You can add a ‘do/while’ loop to delete 5 cubes. Note that you will also have to keep track of the total number of cubes so that the code does not hang. In addition Random.Range() may need to be called many times when there are few cubes. I think you will be okay, but there may be a frame rate drop towards the end of the cube hiding. You might be better off creating a list of the cubes, shuffling it, then deleting cubes in order from the list.

#pragma strict

var goPrefab : GameObject;
var xEdge = 5;  // width
var yEdge = 20;  // height
var zEdge = 1;  // depth
var v3Center = Vector3.zero;
private var total : int;
 
private var arCube : GameObject[,,] = new GameObject[xEdge, yEdge, zEdge];
 
function Start() {
	total = xEdge * yEdge * zEdge;
	
    for (var i = 0; i < xEdge; i++) {
       for (var j = 0; j < yEdge; j++) {
         for (var k = 0; k < zEdge; k++) {
          var x = v3Center.x + xEdge - i - 0;
          var y = v3Center.y + yEdge - j - 1;
          var z = v3Center.z + zEdge - k - 1;
          arCube[i,j,k] = Instantiate(goPrefab, Vector3(x,y,z), Quaternion.identity);
         }
       }
    }
}

function Update() {
	if (Input.GetMouseButtonDown(0)) {
 		var count = 5;
		do {
			if (total <= 0) return;
			
			var Cube : GameObject = arCube[Random.Range(0, xEdge), Random.Range(0, yEdge), Random.Range(0, zEdge)];
 
	       	if (Cube.renderer.enabled) {
	       		Cube.renderer.enabled = false;
	 			count--;
	 			total--;
	    	}
	    } while (count > 0);
	}
}

I have no idea how it is done in javascript but in c# I’d do something like this:

			//Make a new list for cubes to be disabled
			List<GameObject> cubesToDisable = new List<GameObject>();

			while (cubesToDisable.Count <= 10)
			{
				//Select one cube
				var cube = arCube[Random.Range(0, xEdge), Random.Range(0, yEdge), Random.Range(0, zEdge)];
				//If its not already in list, add it. Else discard it and dont increment the counter.
				if (!cubesToDisable.Contains(cube))
				{
					cubesToDisable.Add(cube);
				}
			}
				//Easier way to do a foreach loop on list
			cubesToDisable.ForEach(cube =>
				{
					if (cube.renderer.enabled)
						cube.renderer.enabled = false;
				});

This takes 10 cubes from your array and disables them. But beware, if you don’t have 10 unique cube objects in array, you’ll be stuck in infinite loop.