- Home /
C# Variables Transform vs GameObject
Hi all,
I'm currently working through a tutorial (http://vimeo.com/album/1546437/video/20685520) and am a bit confused about Transform variables vs GameObjects.
I've encountered them in the past and they seem pretty similar, but in this particular tutorial (while doing it in C#) I came across an issue trying to use GameObjects[] as an array and trying to grab the length, whereas it worked fine as Transforms.
If someone could clarify the differences for me, that would be fantastic. I am aware of the analogy of the GameObject being the container and the Transform is a component, I'm just confused why making an array of GameObjects isn't behaving like an array.
Transform[] transArray = Selection.transforms;
GameObject[] goArray = Selection.gameObjects;
int transLength = transArray.Length;
int goLength = goArray.length; // Doesn't recognize this property
Thanks much!
Answer by meat5000 · Sep 18, 2013 at 08:02 PM
int goLength = goArray.length
Needs ; on the end.
Also the line above you have capital L not lowercase, which works. Copy that.
So
int goLength = goArray.Length;
Sorry - I was being lazy. That makes no difference for some reason. When typing the .length, it doesnt even recognize it as a property on the goArray.
I was doing the lower case and ; to illustrate it wasn't grabbing it as an actually property of that variable.
http://docs.unity3d.com/Documentation/ScriptReference/Selection-gameObjects.html
It doesnt say why but this page says it is strongly recommended to use transform version of function. I think you have stumbled upon one of those weird Unity mysteries.
Also it seems that Selection is only for use within the editor, not sure if you are aware of that.
Your answer
Follow this Question
Related Questions
Array.length edited using scripts 2 Answers
Getting the transform from a GameObject list 3 Answers
Keep adding targets to a list 2 Answers
How do I check multiple gameObjects transform positions? 3 Answers