2016-07-23 5 views
1

У меня есть две ошибки:GetRemainingDistance можно назвать только активным агентом, который был размещен на NavMesh

первая ошибка:

MissingComponentException: Существует нет «NavMeshAgent» не прилагается к объекту игры «ThirdPersonController», но скрипт пытается получить доступ к . Вероятно, вам нужно добавить NavMeshAgent к игровому объекту «ThirdPersonController». Или ваш скрипт должен проверить, установлен ли компонент перед его использованием.

Patroll.Update() (в Assets/My Scripts/Patroll.cs: 41)

Patroll.Update находится в файле сценария я создал под названием: Patroll.cs

using UnityEngine; 
using System.Collections; 

public class Patroll : MonoBehaviour { 

    public Transform[] points; 
    private int destPoint = 0; 
    private NavMeshAgent agent; 

    // Use this for initialization 
    void Start() { 

     agent = GetComponent<NavMeshAgent>(); 

     // Disabling auto-braking allows for continuous movement 
     // between points (ie, the agent doesn't slow down as it 
     // approaches a destination point). 
     agent.autoBraking = false; 

     GotoNextPoint(); 

    } 

    void GotoNextPoint() { 
     // Returns if no points have been set up 
     if (points.Length == 0) 
      return; 

     // Set the agent to go to the currently selected destination. 
     agent.destination = points[destPoint].position; 

     // Choose the next point in the array as the destination, 
     // cycling to the start if necessary. 
     destPoint = (destPoint + 1) % points.Length; 
    } 


    void Update() { 
     // Choose the next destination point when the agent gets 
     // close to the current one. 
     if (agent.remainingDistance < 0.5f) 
      GotoNextPoint(); 
    } 
} 

линия 41:

if (agent.remainingDistance < 0.5f) 

Этот сценарий Patroll.cs я перетащил к Иерархии ThirdPersonController ,

Затем после этого у меня есть другая ошибка, и эту ошибку я также имел еще до того, я создал сценарий Patroll.cs:

«GetRemainingDistance» можно назвать только активным агентом, который был помещен на NavMesh. UnityEngine.NavMeshAgent: get_remainingDistance() UnityStandardAssets.Characters.ThirdPerson.AICharacterControl: Обновление() (на активы/Стандартные активы/символов/ThirdPersonCharacter/скрипты/AICharacterControl.cs: 31)

Эта ошибка в скрипте AICharacterControl.cs это сценарий единства, а также связан с ТретьимПерсонателем в иерархии.

Линия 31:

if (agent.remainingDistance > agent.stoppingDistance) 

То, что я пытался сделать так далеко, чтобы исправить это в единстве. Я нажал на меню Компонент> Навигация> Агент NavMesh

Теперь он добавлен в ThirdPersonController агент Nav Nesh, и я могу видеть в инспекторе ThirdPersonController часть Nav Nesh Agent.

Но ошибка/s все еще существует.

Это сценарий AICharacterControl.cs

using System; 
using UnityEngine; 

namespace UnityStandardAssets.Characters.ThirdPerson 
{ 
    [RequireComponent(typeof (NavMeshAgent))] 
    [RequireComponent(typeof (ThirdPersonCharacter))] 
    public class AICharacterControl : MonoBehaviour 
    { 
     public NavMeshAgent agent { get; private set; }    // the navmesh agent required for the path finding 
     public ThirdPersonCharacter character { get; private set; } // the character we are controlling 
     public Transform target;         // target to aim for 


     private void Start() 
     { 
      // get the components on the object we need (should not be null due to require component so no need to check) 
      agent = GetComponentInChildren<NavMeshAgent>(); 
      character = GetComponent<ThirdPersonCharacter>(); 

      agent.updateRotation = false; 
      agent.updatePosition = true; 
     } 


     private void Update() 
     { 
      if (target != null) 
       agent.SetDestination(target.position); 

      if (agent.remainingDistance > agent.stoppingDistance) 
       character.Move(agent.desiredVelocity, false, false); 
      else 
       character.Move(Vector3.zero, false, false); 
     } 


     public void SetTarget(Transform target) 
     { 
      this.target = target; 
     } 
    } 
} 

Я не могу понять, как исправить ошибки.

ответ

0

У меня такая же проблема в моей игре. Это произошло, когда я загружаю сборку символов в течение времени выполнения, что prefab на моей старой карте имеет другую позицию. Чтобы исправить это, вы можете поместить свой сборник в навигационную сетку и сохранить свой сборник.