Could someone explain the best way to avoid getting errors like ‘Object reference not set to an instance of an object’ please? I keep getting this type of error for different stuff and I would really like to know how to avoid it in the future and be able to fix it myself. I feel I am missing something fundamental. When I click on the error it takes me to line ‘int xPos = AIposStore[IDBest].xPos;’ So I am guessing the Array is not properly declared as an object or something?
Below is the script that currently has the error. Please excuse the sloppy code, this is the first time I tried to code some AI logic for a one player grid style puzzle game. Any other optimisations / better coding practices most welcome.
Full error:
NullReferenceException: Object reference not set to an instance of an object
AI.findBestPossition (System.Int32& returnx, System.Int32& returny, System.Int32& returnt, System.Int32& returnl) (at Assets/Scripts/AI.cs:38)
Game.Update () (at Assets/Scripts/Game.cs:79)
using UnityEngine;
using System.Collections;
public class AIpossitions
{
public int xPos;
public int yPos;
public int Gtype;
public int Glegal;
public int howGood;
}
public class AI : MonoBehaviour {
private Game game;
private LegalGirder legalGirder;
private int m_isLegalH;
private bool m_isLegalV;
private int m_GHmoves; // total number of leagle H moves
private int m_GVmoves; // total number of leagle V moves
private int m_Gtotal; // total number of leagle moves
private AIpossitions[] AIposStore = new AIpossitions[1000];
void Start () {
game = GetComponent<Game>();
legalGirder = GetComponent<LegalGirder>();
}
void Update () {
}
public void findBestPossition(out int returnx, out int returny, out int returnt, out int returnl)
{
GameData gameData = game.gameData;
gameData.SetGameAIactive(1);
storePossibleMoves(); // Tests and stores all possible moves into an Array.
testGH_MakeBoxMoves(); // Tests from Array for good possition. If found adds 1 to its 'howGood' score.
int IDBest = findBestScore(); // Find the highest scoring possition from the Array.
returnx = AIposStore[IDBest].xPos;
returny = AIposStore[IDBest].yPos;
returnt = AIposStore[IDBest].Gtype;
returnl = AIposStore[IDBest].Glegal;
}
// Tests and stores all possible moves into an Array.
public void storePossibleMoves()
{
GameData gameData = game.gameData;
int IDcount = 0;
for (var y = 1; y < gameData.GetGridSize(); y++)
{
for (var x = 1; x < gameData.GetGridSize() - 1; x++)
{
m_isLegalH = legalGirder.TestHGirderLegal(x, y);
if (m_isLegalH > 0)
{
AIposStore[IDcount].xPos = x;
AIposStore[IDcount].yPos = y;
AIposStore[IDcount].Gtype = 0;
AIposStore[IDcount].Glegal = m_isLegalH;
AIposStore[IDcount].howGood = 1;
IDcount++;
}
}
}
int m_GHmoves = IDcount - 1;
for (var x = 1; x < gameData.GetGridSize(); x++)
{
for (var y = 2; y < gameData.GetGridSize(); y++)
{
m_isLegalV = legalGirder.TestVGirderLegal(x, y);
if (m_isLegalV == true)
{
AIposStore[IDcount].xPos = x;
AIposStore[IDcount].yPos = y;
AIposStore[IDcount].Gtype = 1;
AIposStore[IDcount].Glegal = 0;
AIposStore[IDcount].howGood = 1;
IDcount++;
}
}
}
int m_GVmoves = IDcount - m_GHmoves - 1;
int m_Gtotal = IDcount - 1;
}
// Tests from Array for good possition. If found adds 1 to its 'howGood' score.
public void testGH_MakeBoxMoves()
{
GameData gameData = game.gameData;
int IDcount = 0;
for (var s = 1; s < m_GHmoves; s++)
{
int testPosX = AIposStore[IDcount].xPos;
int testPosY = AIposStore[IDcount].yPos;
if ((testPosY < gameData.GetGridSize()) && (testPosY > 0))
{
if ((gameData.GetGirdersHState(testPosX, testPosY - 1) >= 1) && (gameData.GetGirdersVState(testPosX, testPosY - 1) == 1) && (gameData.GetGirdersVState(testPosX + 1, testPosY - 1) == 1))
{
AIposStore[IDcount].howGood++;
}
}
}
}
// Find the highest scoring possition from the Array.
public int findBestScore()
{
int IDScore = 0;
int IDBest = 0;
for (var s = 1; s < m_Gtotal; s++)
{
if (AIposStore~~.howGood > IDScore)~~
{
IDScore = AIposStore~~.howGood;
IDBest = s;
}
}
return IDBest;
}
}~~