reference an object without pre-assigning.

Im at a stump because I have had a problem with functions like GetComponent();.

this is a simple example script for reference:

var myTransform : Transform;
 
function Update () {
 
 transform.LookAt(myTransform);
 
}

The script works fine for making my enemies face the player, and all is good. I place the enemy in the scene, attach the script, and set the transform to my player and play it.

What the problem is however, Is I cannot have additional enemies spawn into the scene because The transform will be blank, so the enemy will just shoot at a wall. And having to have every enemy and object on the map have to be pre assigned is tiresome, time consuming and limits thegames capabilities.

What I am wondering, is how to make the first line of the script, or any function looking like this:

var HPtext : GUIText = gameObject.GetComponent(GUIText);

be able to reference an object and target it without having to be pre assigned. How would I go about doing this?

To find your player with any enemy script, you can simply create a tag and set it on your player, in the inspector.

then, to find your player:

Transform player = GameObject.FindGameObjectWithTag("Player");

Does that solves your problem, Eevee?

Thanks for the help anyone who responded to this, I fixed the problem.

var Target : Transform;

function FindTarget () : Transform {
	if(GameObject.FindWithTag("Player")){
		who = GameObject.FindWithTag("Player"); //We have found a Target
	}
	return who.transform;
}

function Update () {
if(!Target){
		Target = FindTarget(); return;
	}
transform.LookAt(Target);

}