- Home /
Compiler error with A.I movement Script
So I have a script that basically makes the enemy object look at the player, and move towards the player if they are a certain distance away. However, I am getting a compiler error saying that "An instance of type 'UnityEngine.CharacterController' is required to access non static member 'Move'." The prefab that this script is attached to has a CharacterController component, so I am not sure what is wrong. Here is the script:
//Inspector variables
var target : Transform;//Target object of the enemy, i.e. "Player" object
var Speed : float;//Speed of the enemy object
var maxMoveDistance : int;// Max Distance allowed between enemy and player before enemy moves towards player
var minMoveDistance : int;//Minimum distance from target before enemy will stop moving
@script RequireComponent(CharacterController)
function Update ()
{
transform.LookAt(target);
var controller : CharacterController = GetComponent(CharacterController);
var dist = (Vector3.Distance(transform.position,target.position));
if (dist > maxMoveDistance && !(dist < minMoveDistance))
{
var dir : Vector3 = target.position;
CharacterController.Move(dir * Speed);
//transform.position = Vector3.MoveTowards(transform.position, target.position, Speed);
}
}
Answer by Eric5h5 · Oct 03, 2012 at 07:13 PM
You need to use GetComponent; without that you can't just write "CharacterController.Move" if Move isn't a static function.
He actually has the GetComponent but ins$$anonymous$$d of using the controller reference he keeps on using the class name.
@clarkes707 use controller.$$anonymous$$ove ins$$anonymous$$d
Answer by vamsi1234 · Oct 03, 2012 at 07:49 PM
change
CharacterController.Move(dir * Speed);
to this and try again
controller.Move(dir * Speed);
Ah ok I see what I did. I need to use the "controller" variable that holds the get component, like you said. Thanks a lot for your help everyone!