- Home /
Any ideas on a simple AI script that doesn't use any drag/drop variables
So I have a spawn script that uses instantiate to spawn an enemy prefab. However the AI script on the enemy uses the position of the player within the hierarchy and instantiated prefabs do not keep these variables. does anybody have any advice on how to create an ai script that doesn't use this but still moves towards the player?
Answer by syclamoth · Jan 31, 2012 at 01:56 PM
If your player object has a tag, you can use
GameObject.FindWithTag(tagName);
to get the first found object with that tag. If you are using the "Player" tag, and make sure that there is only ever one object with it, it should work fine.
Thanks alot, so you think like
var player : GameObject; var playerPosition : Vector3;
function Update (){
player = GameObject.FindWithTag(Player); playerPosition = player.transform.position;
?