Unity: Transforms vs GameObjects

edited in Questions and Answers
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:

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

  • edited
    Just use child.gameObject. :)

    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)
  • Hey Ely,

    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.
  • Hey @Tachyon, any chance you can maybe post the exact error message you get? I'm not an expert, but I'll try to help if I can.
  • doh! facepalm, sublime's autocorrect placed GameObject instead of gameObject. Works now! What's the difference between the two anyway?
  • GameObject is the class, ie the type. gameObject is the instance of the type associated with the transform.
  • A gameobject is the container for all the components that an object has attached to it while the transform simply defines it's place in the world (and therefore deals with parent/child object handling)
  • Right, gotchya @Rigormortis, thanks for the explanation. Since I did not declare it I am assuming that gameObject is an implicit instance in this case?

    @D3zmodos - I think you misunderstood the question but thanks anyway ;)
  • edited
    Er... I don't know the terminology as I'm not a programmer, but unless you create a new Transform in a script, your Transform will always have an associated gameObject reference to the GameObject instance to which it's attached. Like, the gameObject already exists because you've placed/created it in your scene, and no gameObject gets placed without transforms.

    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.
  • edited
    Edit Hmmm weird double post; original removed.
  • edited
    It's impossible to create a Transform that is not attached to a gameObject. (You cannot construct Transform, and any field with a Transform value will be linked via the inspector (which means it will always have a gameObject)).

    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

    public Transform transform
    {
       get
       {
          return GetComponent<Transform>();
       }
    }


    which is supposedly why calls to transform are "slow" and some programmers recommend caching the value.

Sign In or Register to comment.