Unity: Transforms vs GameObjects
Hey, so my cube puzzle replica is coming along and I am stuck again in some code. A friend helped code some logic to iterate through game objects in an empty container object so that cubes would not occupy the same space:
My understanding from this code is that game objects are stored in the transform of the container object?
Now when I call the setBlockPosition function..
..I receive an error because the iTween MoveTo function requires game object information instead transform information. I hope this is making sense? Can I convert a transform into a game object? Thoughts?
private function handleButtonClick(columnIncrementer:int, rowIncrementer:int) { for (var child:Transform in container.transform) { var currentColumn:int = Mathf.Floor(child.position.z / cellSize); var currentRow:int = Mathf.Floor(child.position.x / cellSize); var openColumnIndex:int = currentColumn + columnIncrementer; var openRowIndex:int = currentRow + rowIncrementer; while (checkIfBlockOccupied(openColumnIndex, openRowIndex) == true && openColumnIndex > -1 && openRowIndex > -1 && openColumnIndex < columns && openRowIndex < rows) { openColumnIndex += columnIncrementer; openRowIndex += rowIncrementer; } if (openColumnIndex > -1 && openRowIndex > -1 && openColumnIndex < columns && openRowIndex < rows) { setBlockPosition(child, currentColumn + columnIncrementer, currentRow + rowIncrementer); ....
My understanding from this code is that game objects are stored in the transform of the container object?
Now when I call the setBlockPosition function..
private function setBlockPosition(child:Transform, column:int, row:int) : void { iTween.MoveTo(child, Vector3(row * cellSize, 0, column * cellSize), 2); //child.position.z = column * cellSize; //child.position.x = row * cellSize; }
..I receive an error because the iTween MoveTo function requires game object information instead transform information. I hope this is making sense? Can I convert a transform into a game object? Thoughts?
Comments
You can refer to a GameObject's transforms by calling MyGameObject.transform, and you can refer to a Transform's gameobject by calling MyTransform.gameObject. You could do that endlessly if... well... you wouldn't, but you could. :P (i.e. MyGameObject.transform.gameObject.transform.gameObject == MyGameObject // returns true, far as I know)
child.gameObject doesn't work. I tried that already. It just returns null. It's like the objects are stored in the transform of the container and are not objects themselves.
@D3zmodos - I think you misunderstood the question but thanks anyway ;)
If you created a Transform via script... then gameObject might be null (haven't tested that). I don't know whether that means it isn't implicit. :P
[edit] What Herman said below. :P Just tried, and you can't create a Transform in script.
gameObject and transform (and a few others) are just properties defined in the base class Component (see http://docs.unity3d.com/Documentation/ScriptReference/Component.html). I've heard that these just wrap GetComponent calls, for instance transform could be defined as
which is supposedly why calls to transform are "slow" and some programmers recommend caching the value.