List Null Ref
Hello. I'm trying to create a list of waypoints for an enemy object to move between. I'm doing this by storing a list of waypoint objects, having the enemy move to one, wait, then move to another. The problem is that the list keeps saying it's null in FixedUpdate even after I add game objects to it in Start. I'm not sure what I'm doing wrong here. line 35 "Debug.Log("wayPoints count is " + wayPoints.Count);" is throwing the Null Ref error.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyAI : MonoBehaviour
{
public GameObject player, patrolA, patrolB;
public RaycastHit2D payerSpotter;
private Detector detector;
private PlayerData playerData;
public float idleTime = 3;
public float idleTimer;
private List<GameObject> wayPoints;
private int wayPointsListIndexer = 0;
void Start ()
{
player = GameObject.FindWithTag("Player");
playerData = gameObject.GetComponent<PlayerData>();
List<GameObject> wayPoints = new List<GameObject>();
wayPoints.Add(patrolA);
wayPoints.Add(patrolB);
Debug.Log("waypoint 0 is "+ wayPoints[wayPointsListIndexer].name);
}
void FixedUpdate ()
{
idleTimer -= Time.deltaTime;
Debug.Log("wayPoints count is " + wayPoints.Count);
if (idleTimer <= 0)
{
Debug.Log("wayPointIndexer = " + wayPointsListIndexer);
NextWayPoint(wayPoints[wayPointsListIndexer]);
idleTimer = idleTime;
}
}
void NextWayPoint(GameObject curWayPoint)
{
Debug.Log("curWayPoint is " + curWayPoint.name);
if (gameObject.transform.position == curWayPoint.transform.position)
{
wayPointsListIndexer++;
if (wayPointsListIndexer > wayPoints.Count)
wayPointsListIndexer = 0;
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position,
wayPoints[wayPointsListIndexer].transform.position, playerData.moveSpeed * Time.deltaTime);
}
}
}
Answer by gjf · Nov 09, 2015 at 09:17 PM
you're redefining the waypoints
on line 21 - it's not the same one as you're referencing elsewhere (which you define on line 13)
Your answer
Follow this Question
Related Questions
Number of methods based on dynamic list count? 1 Answer
NullReferenceException : Object reference not set to an instance of an object? 0 Answers
NullReferenceException: When creating a graph 0 Answers
List remains null after initialisation? 1 Answer
Getting "object reference not set" error when creating a list dictionary. 1 Answer