Hi guys !
I am new to scripting and I tried to follow this tutorial and apply it to my own game.
My problem is I have this error in unity :
"NullReferenceException: Object reference not set to an instance of an object
GameManager.SpawnAllWarlocks () (at Assets/Script/GameManager.cs:37)
GameManager.Start () (at Assets/Script/GameManager.cs:27) "
Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public int numRoundsToWin = 5;
public float startDelay = 3f;
public float endDelay = 3f;
public CameraController cameraController;
public Text messageText;
public GameObject warlockPrefab;
public WarlockManager[] warlocks;
private int roundNumber;
private WaitForSeconds startWait;
private WaitForSeconds endWait;
private WarlockManager roundWinner;
private WarlockManager gameWinner;
void Start () {
startWait = new WaitForSeconds(startDelay);
endWait = new WaitForSeconds(endDelay);
SpawnAllWarlocks();
SetCameraTargets();
StartCoroutine(GameLoop());
}
private void SpawnAllWarlocks()
{
for (int i = 0; i < warlocks.Length; i++)
{
warlocks[i].instance = Instantiate(warlockPrefab, warlocks[i].spawnPoint.position, warlocks[i].spawnPoint.rotation) as GameObject;
warlocks[i].playerNumber = i + 1;
warlocks[i].Setup();
}
}
private void SetCameraTargets()
{
Transform[] targets = new Transform[warlocks.Length];
for (int i = 0; i < targets.Length; i++)
{
targets[i] = warlocks[i].instance.transform;
}
cameraController.m_Targets = targets;
}
private IEnumerator GameLoop()
{
yield return StartCoroutine(RoundStarting());
yield return StartCoroutine(RoundPlaying());
yield return StartCoroutine(RoundEnding());
if (gameWinner != null)
{
SceneManager.LoadScene(0);
}
else
{
StartCoroutine(GameLoop());
}
}
private IEnumerator RoundStarting()
{
ResetAllWarlocks();
DisableWarlockControl();
//cameraController.SetStartPositionAndSize();
roundNumber++;
messageText.text = "ROUND " + roundNumber;
yield return startWait;
}
private IEnumerator RoundPlaying()
{
EnableWarlockControl();
messageText.text = string.Empty;
while (!OneWarlockLeft())
{
yield return null;
}
}
private IEnumerator RoundEnding()
{
DisableWarlockControl();
roundWinner = null;
roundWinner = GetRoundWinner();
if (roundWinner != null)
roundWinner.wins++;
gameWinner = GetGameWinner();
string message = EndMessage();
messageText.text = message;
yield return endWait;
}
private bool OneWarlockLeft()
{
int numWarlockLeft = 0;
for (int i = 0; i < warlocks.Length; i++)
{
if (warlocks[i].instance.activeSelf)
numWarlockLeft++;
}
return numWarlockLeft <= 1;
}
private WarlockManager GetRoundWinner()
{
for (int i = 0; i < warlocks.Length; i++)
{
if (warlocks[i].instance.activeSelf)
return warlocks[i];
}
return null;
}
private WarlockManager GetGameWinner()
{
for (int i = 0; i < warlocks.Length; i++)
{
if (warlocks[i].wins == numRoundsToWin)
return warlocks[i];
}
return null;
}
private string EndMessage()
{
string message = "DRAW!";
if (roundWinner != null)
message = roundWinner.coloredPlayerText + " WINS THE ROUND!";
message += "\n\n\n\n";
for (int i = 0; i < warlocks.Length; i++)
{
message += warlocks[i].coloredPlayerText + ": " + warlocks[i].wins + " WINS\n";
}
if (gameWinner != null)
message = gameWinner.coloredPlayerText + " WINS THE GAME!";
return message;
}
// Update is called once per frame
private void ResetAllWarlocks()
{
for (int i = 0; i < warlocks.Length; i++)
{
warlocks[i].Reset();
}
}
private void EnableWarlockControl()
{
for (int i = 0; i < warlocks.Length; i++)
{
warlocks[i].EnableControl();
}
}
private void DisableWarlockControl()
{
for (int i = 0; i < warlocks.Length; i++)
{
warlocks[i].DisableControl();
}
}
}
From what I’ve read so far, it seems that warlocks.Length returns null but I can’t be so sure since
if (warlocks.Length == null)
Debug.Log("Hello");
returns nothing in console.
From this point, I don’t know how to debug this.
May you help me please ?