NullReferenceException in FiniteStateMachine with ThirdPersonCharacter
Hey guys,
I'm receiving an error when I try to move an enemy character from one waypoint to another. I have a Finite State Machine that changes states for the enemy (currently only WAIT and PATROL). It is started as a coroutine. My goal is that the enemy walks to waypoint 1 (state = PATROL) then waits for a while (state = WAIT) (patrolWaitingTime) and walks to the next waypoint (state = PATROL).
private void Update() {
target = wayPoints[wayPointIndex];
if (target != null) {
agent.SetDestination(target.position);
}
if (state == State.WAIT) {
elapsedWaitingTime += Time.deltaTime;
}
}
private void Patrol() {
if (agent.remainingDistance > agent.stoppingDistance) {
character.Move(agent.desiredVelocity, false, false);
} else {
state = State.WAIT;
}
}
private void Wait() {
character.Move(Vector3.zero, false, false);
if (elapsedWaitingTime > patrolWaitingTime) {
wayPointIndex++;
if (wayPointIndex == wayPoints.Length) {
wayPointIndex = 0;
}
elapsedWaitingTime = 0;
state = State.PATROL;
}
}
Sorry for the long Code but I cannot really locate the error. The problem occurs when it first calls "character.Move" in the Wait-function and it goes back to the standard asset script "ThirdPersonCharacter" line 215:
m_Animator.applyRootMotion = true;
My guess is that it has something to do with the coroutine but I'm a little lost there...I got the full Code attached here and a picture so visualize it :) I really appreciate any help.
Cheers, Dan
Your answer
Follow this Question
Related Questions
Static function not working in between screen 2 Answers
Unity Navmesh agent patrol and chase player script issues. 0 Answers
getting a NullReferenceException error and can't see why. 1 Answer
Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer
Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers