Assigning GameObjects to variables

Hey guys,

I’m having trouble assigning a gameobject to a variable…
At the moment I’ve done this

cubeArray[x][y][z] = GameObject.CreatePrimitive(PrimitiveType.Cube);

Which I thought was all brilliant… But I’ve just now realised when I do anything to cubeArray elements, for example

cubeArray[0][0][0].transform.position = positionVector;

It accepts it as a valid execution, but does nothing to the actual gameObject.

I figure it’s making 2 copies, the variable and the one that’s in the the actual game-engine or something.

Is there any feasible way of doing this?

Thanks for any help guys,
Stephen

Can you give a bit more information about what you are trying to do here? If you can post the whole script, it will help narrow down the problem.

Hey, sure. I’m a little new to Unity scripting so my code is probably terribly inefficient :expressionless:

So basically I’ve got this script that generates a 3x3 array of cubes, as children of the GameObject that the script is attached to.

var cubeArray = new Array(new Array(new Array()));
function Start () {
    gameObject.AddComponent("Rigidbody");
    for(var x = 0; x < 23; x++){
        cubeArray[x] = new Array(new Array());
        for(var y = 0; y < 11; y++){
            cubeArray[x][y] = new Array();
            for(var z = 0; z < 23; z++){
                cubeArray[x][y][z] = GameObject.CreatePrimitive(PrimitiveType.Cube);
                Destroy(cubeArray[x][y][z].GetComponent(MeshRenderer));
                Destroy(cubeArray[x][y][z].GetComponent(MeshFilter));
                cubeArray[x][y][z].name = "pathFindingObject";
                cubeArray[x][y][z].GetComponent("BoxCollider").isTrigger = true;
                cubeArray[x][y][z].AddComponent("pathFindingMeshTrigger");
                cubeArray[x][y][z].transform.localScale = Vector3(50, 50, 50);
                cubeArray[x][y][z].transform.position = transform.position+Vector3((x-11)*50, (y-1)*50, (z-11)*50);
                cubeArray[x][y][z].transform.parent = transform;
                Destroy(cubeArray[x][y][z].GetComponent("pathFindingMeshTrigger"));
            }
        }
    }
    Destroy(gameObject.GetComponent("Rigidbody"));
}

the “pathFindingMeshTrigger” script checks if that cube is touching anything (namely the GameObject the cubes are children of), and if it is - it deletes it.

But this is where I’m getting problems, my script for deleting the cube is…

function OnTriggerEnter (myTrigger:Collider) {
    Destroy(gameObject);
}

But if, say, cubeArray[0][0][0] was hitting something, it deletes it as an object in Unity - but cubeArray[0][0][0] still exists, containing a GameObject as its value.

Is there any way to do what I’m trying to do here? Am I doing something stupid and missing the obvious route?

Do you actually need to remove the array element itself? If so, you need to use Array.RemoveAt. The array element is actually a reference to an object in the scene. If you destroy the scene object, the array element will still exist but it will be nulled out if you try to use it.

That’s what I mean though - I destroy the scene object, but then call Debug.Log() on the array element which contained the now destroyed scene object, and it tells me that it still exists as a game object?