How do you access the transform component of an object in another class?

Here is my AI script that I have attached to my enemy.

public class AI : MonoBehaviour {

 public Enemy enemy;

void Start () {
enemy = new Enemy();
float distance = enemy.Distance();

public class Creature : MonoBehaviour{

public float Distance(){

//I can’t seem to access transform of the object this script is attached to.

float targetDistance = Vector3.Distance(new Vector3(0,0,0), transform);

return targetDistance;



public class Enemy : Creature{


For starters, you must never instantiate components with the new keyword. Enemy derives from Creature which in turns derives from MonoBehavior, i.e. it is a script component. Such must be added using GameObject.AddComponent, or they get instantiated on the object if the gameobject is an instantiation of a prefab which is configured to have such a script as a component.

Once there is a GameObject in your hierarchy that has an Enemy script attached to it, you can access it from scripts like this:

Enemy enemy = GameObject.Find("GameObject name here").GetComponent<Enemy>();

Afterwards, to answer your actual question, the transform is a property of MonoBehavior, so every class that derives from MonoBehavior has access to the transform of the GameObject to which it is attached like so:

Transform enemyTransform = enemy.transform;