This error started happening suddenly and I can’t determine why…
On the first frame the game stops with this error:
NullReferenceException: Object reference not set to an instance of an object
TurnManager.AddUnit (TacticsMove unit) (at Assets/Scripts/TurnManager.cs:85)
TacticsMove.Init () (at Assets/Scripts/TacticsMove.cs:46)
PlayerMove.Start () (at Assets/Scripts/PlayerMove.cs:11)
referencing line 85 of the TurnManager script (I cut out irrelevant bit):
if (!gameStarted && (playerList.Count == startingPlayers.Length))
But I can’t for the life of me figure out which variable is causing the error. Am I declaring the List or Array wrong? The strangest thing is that if I unpause from the error, everything works perfectly. Is it simply an issue of timing? I’m a first time coder figuring things out so any advice would be immensely appreciated. I didn’t include the TacticsMove script because I’m assuming the lines referencing it are just pointing to what is calling the function…but I could be very wrong of course
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnManager : MonoBehaviour
{
public static List<TacticsMove> playerList = new List<TacticsMove>();
public static List<TacticsMove> enemyList = new List<TacticsMove>();
public static Queue<TacticsMove> enemyQueue = new Queue<TacticsMove>();
public static int playerCount;
public static bool playerRound = false;
public static GameObject[] startingPlayers;
public static bool gameStarted = false;
// Start is called before the first frame update
void Start()
{
startingPlayers = GameObject.FindGameObjectsWithTag("Player");
Debug.Log("THERE ARE " + startingPlayers.Length + " PLAYERS");
}
public static void AddUnit(TacticsMove unit)
{
if (unit.tag == "Player")
{
playerList.Add(unit);
Debug.Log(unit + " ADDED");
if (!gameStarted && (playerList.Count == startingPlayers.Length))
{
InitPlayerRound();
gameStarted = true;
//Debug.Log("GAME START");
}
}
else
{
enemyList.Add(unit);
Debug.Log(unit + " ADDED");
}
}