Add item into list<>

I have the following code -

public List<Achievment> Achievments = new List<Achievment>();

[System.Serializable]
public class Achievment
{
	public string Name;
	public string Description;
	public string Img;
	public int Xp;
	public int Credits;
	public string Board;
	public string Deck;
	public string Require;
	public int Count;
	public bool Completed;

	public Achievment(string nam, string des, string img, int xp, int cre, string boa, string dec, string req, int cou, bool com)
	{
		Name = nam;
		Description = des;
		Img = img;
		Xp = xp;
		Credits = cre;
		Board = boa;
		Deck = dec;
		Require = req;
		Count = cou;
		Completed = com;
	}

}

and am trying to add item into the sublist by using

AchievmentsManager.Instance.Achievments.Add(new Achievment("","","",0,0,"","","",0,false));

but i get NullReferenceException: Object reference not set to an instance of an object.

Any ideas as i just cant see what im doing wrong??

Check each of those objects used in that line to find out which one is null?

sorry for being dumb but i don’t get what you mean.

i am trying to add an item(achievement) to the List<> of achievements.

can you clarify please.

Do this. For each object in the line of code above, use Debug to print it out to the console, and determine which one is null.

ok im feeling really dumb right now…

objects??

i have an empty list called Achievments and with that list i have a SubList called Achiement.

currently the lists are empty and im trying to add into the achivmenst list an achievment. - Achievments.Add(new Achievment(“”,“”,“”,0,0,“”,“”,“”,0,false)

So basically the list effectively is null which why im trying to add to it.

so what am i missing when you say check every object??

AchievmentsManager.Instance.Achievments.Add(new Achievment(“”,“”,“”,0,0,“”,“”,“”,0,false));

–AchievmentsManager.Instance could be null
–AchievementsManager.Instance.Achievments could be null

here is the full script

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AchievmentsManager : MonoBehaviour {

	public static AchievmentsManager Instance;

	//list of achievments
	public List<Achievment> Achievments = new List<Achievment>();

	// Use this for initialization
	void Start () 
	{
		Instance = this;
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
}

[System.Serializable]
public class Achievment
{
	public string Name;
	public string Description;
	public string Img;
	public int Xp;
	public int Credits;
	public string Board;
	public string Deck;
	public string Require;
	public int Count;
	public bool Completed;

	public Achievment(string nam, string des, string img, int xp, int cre, string boa, string dec, string req, int cou, bool com)
	{
		Name = nam;
		Description = des;
		Img = img;
		Xp = xp;
		Credits = cre;
		Board = boa;
		Deck = dec;
		Require = req;
		Count = cou;
		Completed = com;
	}

}

so …
AchievmentsManager.Instance = the script
AchievementsManager.Instance.Achievments = the empty list

so how do i check a script is empty or null and an empty list is empty or null???

im sorry but i am totally missing the point here so let me re-ask the question

How do i add an achievment into the achievments list in c# script.

if (AchievmentsManager.Instance.Equals(null))
{
Debug.log(“Instance is null”);
}

etc…

ok i just did

if (AchievmentsManager.Instance.Equals(null))
{
Debug.Log(“Instance is null”);
}

if (AchievmentsManager.Instance.Achievments.Equals(null))
{
Debug.Log(“Instance.Achievments is null”);
}

and got no debug errors…

so…

How do i add an achievement into the achievements list in c# script.

Script execution order might also be a problem here.
Try assigning “Instance” in Awake instead of Start

this was the answer and totally not what i was expecting.

this is a gotcha i will watch out for in future.

thank you.

Ok i now have a new problem which is how do add too, access a list within a list.

i have searched all over the net but cant find the right syntax that works.

my code is -

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BoardManager : MonoBehaviour {

	public static BoardManager Instance;

	//list of boards owned by player
	public List<Boards> OwnedBoards = new List<Boards>();

	//board chosen for current game
	public Boards CurBoard;

	void Awake () 
	{
		Instance = this;
	}

	// Use this for initialization
	void Start () 
	{

	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
}
[System.Serializable]
public class Boards
{
	public string Name;
	public string Description;
	public Enviroments Enviroment;
	public string Img;
	public GameObject Prefab;
	public bool Night;
	public List<BackGroundScene> BackGround = new List<BackGroundScene>(); // list of player
	public List<Cells> cell = new List<Cells>();
	
	public enum Enviroments 
	{
		DenseFog,
		Raining,
		Stromy,
		Cloudy,
		Sunshine
	}

	public Boards(string nam, string des, string img, bool nig)
	{
		Name = nam;
		Description = des;
		Img = img;
		Night = nig;
	}

}


[System.Serializable]
public class Cells
{
	public string color;
	public CellType cellType;
	public bool Trapped;
	public string trappedByPlayerName;
	public int Card;

	public enum CellType 
	{
		none,
		drawcard,
		special,
		events
	}

	public Cells(string col)
	{
		color = col;
	}
}

[System.Serializable]
public class BackGroundScene
{
	public string Name;
	public bool IsLimbo;
	public GameObject ScenebackGround;
	
}

and im trying to add with-

BoardManager.Instance.OwnedBoards[Cells].Add(new Cells(Itemss.Attributes[“colour”].Value.ToString()));

i know its a syntax thing, i just cant find it

Looking at your class, the List property is called cell, so I would think

BoardManager.Instance.OwnedBoards.cell.Add(new Cells(Itemss.Attributes[“colour”].Value.ToString()))

Would do the trick.

that was my 1st thought but i don’t get cell or cells and i get the error as i type out the line which is what i would expect.

error CS1061: Type System.Collections.Generic.List<Boards>' does not contain a definition for cell’ and no extension method cell' of type System.Collections.Generic.List’ could be found (are you missing a using directive or an assembly reference?)

so im stuck trying to work out how i actually access the list within a list

BoardManager.Instance.OwnedBoards[IndexOfABoard].cell.Add(new Cells(Itemss.Attributes[“colour”].Value.ToString()));

Your naming convention is very confusing.
Your class is called cells (plural), and a list of cells is called cell (singular)?

public Boards CurBoard;
Your current board (singular) is of type boards (plural)?

thanks that cleared that issue up.

i have intergers in an xml to store the enum and am using

CellTypes CellType = (CellTypes)ctype;

the interger is passed ok but the enum is not getting chosen.

this is the associated code.

[System.Serializable]
public class Cells
{
	public string color;
	public CellTypes CellType;
	public bool Trapped;
	public string trappedByPlayerName;
	public int Card;

	public enum CellTypes 
	{
		none = 0,
		drawcard = 1,
		special = 2,
		events = 3
	}

	public Cells(string col, int ctype)
	{
		color = col;
		CellTypes CellType = (CellTypes)ctype;
		//CellTypes CellType = (CellTypes)Enum.Parse(typeof(CellTypes), ctype);
		Debug.Log(ctype);
	}
}

sorry to ask so many questions but this area of my code is giving me major hassle.