Question by
kyle-hope · Mar 07, 2019 at 08:36 PM ·
scripting problemaierror messagewaypointswaypoint system
Error with AI going to Waypoints,Issues with AI tracking waypoints
Hello, I have been trying to develop my own AI based off of the Unity Pluggable AI lesson - however I've been running into a bit of an issue when trying to implement some sort of waypoint system to my AI. The error I keep getting is:
NullReferenceException: Object reference not set to an instance of an object
EnemyManager.SetupAI (System.Collections.Generic.List`1 wayPointList) (at
Assets/EnemyManager.cs:25)
GameManager2.SpawnAI (System.Collections.Generic.List`1 waypoints) (at
Assets/GameManager2.cs:28)
GameManager2.Start () (at Assets/GameManager2.cs:18)
I've been trying to fix this, however no matter what I've done I haven't figured it out and I'm out of ideas. Below is the c# scripts for three of my classes with (the three associated with the making the AI and the error itself), any help would be appreciated, thanks!
GameManager2 Class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager2 : MonoBehaviour {
public GameObject[] m_EnemyPrefabs;
public EnemyManager[] m_Enemy;
public List<Transform> wayPointsForAI;
private WaitForSeconds m_StartWait;
void Start () {
m_StartWait = new WaitForSeconds (1);
SpawnAI (wayPointsForAI);
StartCoroutine (GameLoop ());
}
private void SpawnAI(List<Transform> waypoints){
for (int i = 0; i < m_Enemy.Length; i++) {
m_Enemy [i].m_Instance = Instantiate (m_EnemyPrefabs [i], m_Enemy [i].m_SpawnPoint.position, m_Enemy [i].m_SpawnPoint.rotation) as GameObject;
m_Enemy [i].m_PlayerNumber = i;
m_Enemy [i].SetupAI(waypoints);
}
}
}
EnemyManager:
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.AI;
[Serializable]
public class EnemyManager {
public Transform m_SpawnPoint;
[HideInInspector] public int m_PlayerNumber;
[HideInInspector] public GameObject m_Instance;
[HideInInspector] public List<Transform> m_WayPointList;
private StateController m_StateController;
private EnemyMovement m_Movement;
private GameManager2 m_GameManager;
public void SetupAI(List<Transform> wayPointList){
m_StateController = m_Instance.GetComponent<StateController> ();
m_StateController.SetupAI(true, wayPointList);
MeshRenderer[] renderers = m_Instance.GetComponentsInChildren<MeshRenderer> ();
}
StateManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class StateController : MonoBehaviour {
public State currentState;
public State remainState;
public Transform eyes;
[HideInInspector] public NavMeshAgent navMeshAgent;
[HideInInspector] public List<Transform> wayPointList;
[HideInInspector] public int nextWayPoint;
[HideInInspector] public Transform chaseTarget;
[HideInInspector] public float stateTimeElapsed;
private bool aiActive;
void Awake(){
navMeshAgent = GetComponent<NavMeshAgent> ();
}
public void SetupAI(bool aiActivation, List<Transform> wayPoints)
{
wayPointList = wayPoints;
aiActive = aiActivation;
if (aiActive)
{
navMeshAgent.enabled = true;
} else
{
navMeshAgent.enabled = false;
}
}
Comment