Having trouble with Transform and GameObject.

I’m having trouble with this whole “Transform/GameObject” thing. I don’t really understand how to implement without mixing those two up. For example I uses Transform to calculate distance between my player and the enemy. On the other hand my AutoTarget script uses GameObject to locate the closest enemy to target. And I get mixed up in my “var target”. I got stucked there. Would anyone help me with this?

PlayerAttack.js script

var target : Transform;

var damage : float = 10;

var myTransform : Transform;

var attackThreshold = 3;

var attackRepeatTime = 1;

var projectile : GameObject;

private var attackTime = Time.time;

function Awake()

{
myTransform = transform;
}

function Update ()

{

target = GetComponent(AutoTarget).target;
var distance = (target.position - myTransform.position).magnitude;
var hit:RaycastHit;

if(distance < attackThreshold && Time.time > attackTime)
{
	Instantiate (projectile, transform.position, transform.rotation);
	attackTime = Time.time + attackRepeatTime;
	hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}

}

AutoTarget.js script

var target = closest;

var closest : GameObject;

var Speed : float;

var Turn : float;

function Update () {

target = closest;

var enemies : GameObject[];

enemies = GameObject.FindGameObjectsWithTag("Enemy");

var distance = Mathf.Infinity;

var position = transform.position;

for(var enemy : GameObject in enemies)
{
	var diff = (enemy.transform.position - transform.position);
	var curDistance = diff.sqrMagnitude;
	if(curDistance < distance)
	{
		closest = enemy;
		distance = curDistance;
	}
}

transform.rotation = Quaternion.Slerp(transform.rotation,
	Quaternion.LookRotation(closest.transform.position - transform.position), Turn*Time.deltaTime);
transform.position += transform.forward*Speed*Time.deltaTime;
return closest;

}

All of this is specific to Unity, but I figure it’s a handy way to think of things:

You can more or less think of a GameObject as a collection of components, each of which does various things. There are rigidbodies and colliders, which provide physics simulation; renderers, which draw things in the world; behavior scripts, which allow you to create your own unique gameplay; there are many different component types. All of these components work together to make a game.

A Transform is a particular type of component which represents position, scale, and rotation. It can also contain information about a parent and/or child transforms, allowing you to create a “hierarchy” of objects. It’s probably the most important component in all of Unity.

As far as I know, every GameObject has a Transform component, and every Transform is attached to a GameObject. They’re closely related, and it’s easy to get a reference from one to the other, but they are not the same thing.

//suppose you have a GameObject, GO
//here is a reference to its Transform:
GO.transform

//suppose you have a Transform, T
//here is a reference to its GameObject:
T.gameObject

I’m assuming the problem in your script relates to some confusion between Transform and GameObject. Using the references above, as needed, you should be able to fix the issue.