using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Database : MonoBehaviour {
UI ui;
//Class that stores each tecnology status
[System.Serializable]
public class TechStatus
{
public List<int> techStatus; //List of technologies to see if its researched or not (applied to every company)
}
public int companies = 5; //Number of companies
public int technologies = 10; //Number of technologies
//Company
public List<string> companyName; //List of company names
public List<int> companyMoney; //List of each companys money
public List<int> companyResearchIncome; //List of each companys turn advancment**
//Research
public List<int> researchingTech; //Researching tech (ID of tech)
public List<int> researchProgress; //How much left to complete currently researching technology (from tech cost to 0)
public List<TechStatus> techStatus; //Each companys tech list
public List<int> researchQue; //Which technology goes next (player)
void Start ()
{
ui = gameObject.GetComponent<UI>();
//simulation for each company
for (int i = 0; i < companies; i++)
{
//Company
companyName.Add("Tvrtka " + i);
companyMoney.Add(Random.Range(0, 100));
companyResearchIncome.Add(Random.Range(50, 1000));
//Research
researchingTech.Add(Random.Range(0, 100));
researchProgress.Add(Random.Range(1000, 5000));
TechStatus techStatusClass = new TechStatus();
for (int j = 0; j < technologies; j++)
{
techStatusClass.techStatus.Add(0);
techStatusClass.techStatus.Add(0);
}
techStatus.Add(techStatusClass);
researchQue.Add(Random.Range(0, 100));
}
ui.DrawUI();
}
}
I made some basic data just to test some things but at end part with TechStatus Class and adding things to list says:
NullReferenceException: Object reference not set to an instance of an object
Database.Start () (at Assets/Scripts/Database.cs:50)
have no idea why it doesnt work because yesterday it worked.
My guess is techStatus is null. I’m not seeing where you are initializing your list.
TechStatus techStatusClass = new TechStatus();
for (int j = 0; j < technologies; j++)
{
techStatusClass.techStatus.Add(0);
techStatusClass.techStatus.Add(0);
}
So the techStatus within is null probably.
So you’ll need to do
if(techStatusClass.techStatus == null)
{
techStatusClass.techStatus = new List<int>();
}
Is one way to do it. This will check if it’s initialized before it tries to add something to it. You can also initialize the list when you create the techStatusClass variable if you wanted.
TechStatus techStatusClass = new TechStatus() { techStatus = new List<int>() };
Wow! Yeah it worked!
I didnt add =new List in other lists like companyName and others so didnt know why i should add it in this class i thought initialization was done when i made
TechStats techStatsClass = new TechStats();
seriolusly no idea why in same cases works and others dont, maybe because its from another class
dont quite understand the “parameter-less” constructor, you made new method just to construct variable? Btw my class in list is serializable right?
public class TechStatus
{
public List<int> techStatus;
public TechStatus()
{
techState = new List<int>();
}
}
That is a constructor that takes no parameters. Basically when you create a new instance of TechStatus using new TechStatus(); You aren’t passing in any parameters, so it will use that constructor to initialize the List.
Just to close the loop - if you don’t explicitly declare a constructor then a parameter-less one is created for you. You need to have one for serializable objects because that’s how the serializer creates a new empty instance to assign values to. That’s why I mentioned doing the list initialization there since it fit with the other stuff you were trying to do.