- Home /
How to find target the LookAt with a prefab?
Okay i have an enemy prefab that gets spawned by a collider. I want the prefab to look at the player and eventually fire missiles. However, i've come across a problem where i can't attach the player Transform to the prefab so i'm trying to find a way around it by using "Find" or "FindWithTag" however, none are working very well
var player : Transform;
function Start (){
player = Transform.FindWithTag("Player");
}
function Update () {
transform.LookAt (player);
}
I've tried variations of GameObject, Transform, Component but none are working with each other. Is there another way around this?
Answer by aldonaletto · Dec 02, 2011 at 05:01 PM
You must first make sure the player's tag is set to "Player" (it's one of the standard tags). Find the player at Start with this code:
var player: Transform;
function Start(){ player = GameObject.FindWithTag("Player").transform; } FindWithTag is one of the GameObject functions, and returns a GameObject, but you can get its transform like above.
Hey, thanks for the reply I didn't realise that i could add ".transform" to the end because i kept getting an error stating that it couldn't make the Transform into GameObject so i gave up on that method. But awesome it works! Thanks again for your help
Your answer
Follow this Question
Related Questions
Look At Target? 2 Answers
How to make a GameObject separate from a prefab? 0 Answers
Find right mesh for collider 1 Answer
Find the first game object created with a given name 1 Answer