I’m trying to put each cylinder object in an array and then read it’s coordinates. I guess it should look like this but what’s wrong with my script? error log says “‘position’ is not a member of ‘Object’” but on the other hand transform.Find(“Cube”).position is valid expression. Why’s that?
#pragma strict
function Start () {
}
function Update () {
var row = new Array();
var number = 2;
row[1] = transform.Find(“Cylinder1”);
row[2] = transform.Find(“Cylinder2”);
row[3] = transform.Find(“Cylinder3”);
row[4] = transform.Find(“Cylinder4”);
print(row[number].position);
}
Spelling error perhaps? Check to make sure your row[ ] values are not null after you have assigned them. If they are, it means your Find() function is failing to find your object.
Also, just as an FYI, you probably don’t want to use Find in the Update call, because it means every single frame the engine is having to do a text search (4 times in your case) which can be very slow. Better to cache those values and put the finds in the Start function. I realize this is probably just test code, not meant to be final. But just make sure in your final code you aren’t killing your performance like that
[EDIT]: On second thought, I’m not sure
row[1] = transform.Find(“Cylinder1”);
is doing what you think it is. Transform.Find() is about finding child object in a hierarchy. I don’t know that you can use it the way you are trying to do it (from a global scope).
This function worked fine for me:
void Update () {
Transform[ ] row = new Transform[5];
var number = 2;
row[1] = GameObject.Find(“Cylinder1”).transform;
row[2] = GameObject.Find(“Cylinder2”).transform;
row[3] = GameObject.Find(“Cylinder3”).transform;
row[4] = GameObject.Find(“Cylinder4”).transform;
print(row[number].position);
}
As long as I had those game objects in my scene. If I removed Cylinder2, I got Null Reference Exceptions. On the other hand, when I tried your method, it didn’t work (got a null reference even with the cylinder in place).