- Home /
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?
it's not really how you do $$anonymous$$ecraft. there are a huge number of questions on here to help wit this, please search. there is a complete "$$anonymous$$ecraft starter package" that you only have to download and use.
I'm not trying to apply this to $$anonymous$$ecraft. It's for a school project on data visualization, so any possible way to make this work would be highly appreciated.
Answer by robertbu · Jun 09, 2013 at 02:42 PM
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);
}
}
Thank you so much for the quick reply, this works perfectly! I was thinking it could be solved with the while loop, but couldn't quite figure out how to get it work.
Answer by DuckOfDoom · Jun 09, 2013 at 02:47 PM
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.
Thank you for your input! I'm not familiar with c#, but thankfully robertbu's answer suits my needs perfectly :)
Your answer

Follow this Question
Related Questions
Three Spots For Three Random Objects 1 Answer
Generating a Displacement Map in Unity3d? 1 Answer
Lightning with random intervals 2 Answers
Moving GameObject to various position ? 1 Answer
random limitation memory game 0 Answers