Hello, everybody! Now I'm having a little problem with my scripting. This is my code of my AI.
//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 :
So how can I fix that? Thanks in advance!
asked Sep 22, 2011 at 06:24 AM
You're almost there. :)
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:
Then you're accessing the transform of the GameObject tagged "Player", and not just trying to save the reference to the GameObject itself.
answered Sep 22, 2011 at 06:29 AM