【发布时间】:2015-01-12 12:47:54
【问题描述】:
我收到一个错误“IndexOutOfRangeException: Array index is out of range. NavAgent.FindDestination()”我对 C# 还很陌生,我之前没有使用过数组,所以我 我不太确定我的问题是什么。
任何帮助将不胜感激,在此先感谢您!
这是我的完整脚本:
using UnityEngine;
using System.Collections;
public class NavAgent : MonoBehaviour {
NavMeshAgent myNavAgent;
[SerializeField]
PathNode[] myPathNodes;
int navIndex = 0;
// Use this for initialization
void Start () {
myNavAgent = GetComponent ("NavMeshAgent") as NavMeshAgent;
navIndex = 0;
FindDestination ();
}
// Update is called once per frame
void Update () {
}
void FindDestination()
{
Vector3 newTravelPosition = myPathNodes [navIndex].transform.position;
myNavAgent.SetDestination (newTravelPosition);
}
void OnTriggerEnter()
{
++navIndex;
if (navIndex >= myPathNodes.Length)
navIndex = 0;
FindDestination ();
}
}
【问题讨论】:
-
你没有初始化
myPathAgent
标签: c# arrays indexing unity3d