Hi, I’m having some problem with a part of a code I’m currently writing in C#.
See, I have this list
private List<GameObject> selectedPlanetNodes;
And I want to add a GameObject to it during this method
void SetStartingPlanet()
{
// Get random number between 0 < number of planets
int randomNumber = Random.Range(0, planetNodeList.Count - 1);
Debug.Log(planetNodeList.Count);
// Set nodes to planet of random number Set starting planet to selected
m_currStartNode = planetNodeList[randomNumber];
m_prevSelectNode = m_currStartNode;
Debug.Log(m_currStartNode.name);
Debug.Log(m_currStartNode.gameObject);
//Debug.Log(selectedPlanetNodes.Capacity);
//Debug.Log(selectedPlanetNodes.Count);
selectedPlanetNodes.Add(m_prevSelectNode); <----- [B][COLOR="#FF0000"]ERROR OCCURS HERE!![/COLOR][/B]
m_currStartNode.GetComponent<script_PlanetNode>().IsSelected = true;
// Set random goal planet
do
{
SetGoalPlanet();
} while (m_currGoalNode == m_currStartNode);
}
The SetStartingPlanet() method is called in the start method. Now, the problem I’m facing is that when I try to run the code I get a error saying NullReferenceException: Object reference not set to an instance of an object. I have tried a million things and I can’t get it to work. Even weirder is that when I made sure to initialize the list in the start () method (using selectedPlanetNodes = new List(); ), the only thing that happened was that now Unity crashes every time i run the scene.
Would be thankful for any help.