So basically. I was working on Model in my game and after I finished it I wanted to test it out. Guess what. It didn't work and my teacher said, that he doesn't see any mistake. There are 3 classes involved in this problem.
1st class - nothing difficult (the instance I'm trying to add into the list):
public class MaterialIngot
{
public MaterialIngot(string material,string type, string path, int id, int price, bool melted)
{
Material = material;
Type = type;
Path = path;
ID = id;
Price = price;
Melted = melted;
}
public string Material { get; set; }
public string Type { get; set; }
public string Path { get; set; }
public int ID { get; set; }
public int Price { get; set; }
public bool Melted { get; set; }
}
2nd class - static class with static fields. To keep it simple I'm adding just the code we need to solve this.
public static class ShopTempData
{
public static List<MaterialIngot> tempMaterialIngots { get; set; }
}
3rd class - Monobehaviour class, which is attached to the button.
public class ShopHandler : MonoBehaviour
{
[Header("Input")]
public string Material;
public string Type;
public int Price;
private string Name;
private int AllCost;
public void AddItem()
{
Name = Material + Type;
switch (Type)
{
case "Ingot":
//Debug.Log(Paths.ingotPaths[Name]); //this works
var x = new MaterialIngot(GetComponent<ShopHandler>().Material, GetComponent<ShopHandler>().Type, Paths.ingotPaths[Name], Pdata.ID + 1, GetComponent<ShopHandler>().Price, false);
Debug.Log(x.ID);
Debug.Log(x.Material);
Debug.Log(x.Type);
Debug.Log(x.Path);
Debug.Log(x.Price);
Debug.Log(x.Melted);
ShopTempData.tempMaterialIngots.Add(x); //ERROR IS HERE - Unity says that..
Pdata.ID += 1;
Debug.Log("Added");
Debug.Log(ShopTempData.tempMaterialIngots);
break;
}
//Aktualizace ceny
AllCost += Price;
}
}
As you can see it's not that complicated. I simply call the function, make an instance, where the data ARE contained (the Debug.Log() prints the data correctly) and then I'm trying to add this instance into the static list.
This shows every time (pic): ERROR MESSAGE
If you have any idea, what's wrong. Any advice would be highly appreciated.