Hello, everybody! Now I’m having a little problem with my scripting. This is my code of my AI.
var target : Transform;
function Awake ()
{
if (!target)
{
target = GameObject.FindWithTag ("Player");
}
}
//the code above has error. “Cant Convert GameObject to Transform”. In my function update, I need to use the transform in order to make my AI rotate and move toward the Player.
i.e :
var relativePos = target.position - transform.position;
Your problem is, as the error points out, that GameObject.FindWithTag returns a GameObject, and you want a transform. Fortunately, GameObjects always have a transform. (Since, understandably enough, it’s hard to define an object in your game with less detail than at least a position, scale and rotation).
Therefore, to acquire the player’s transform, all you have to do is add “.transform” to that:
wow!!! Thank you!!! It works!!! ^^
– henry96CHPedersen. Thank you so much for you're help and Henry for asking the question this just saved a ton of trail and error. Thank you.
– ownerfate