i have a class with the characteristics of a player (ID, name, points… etc.)
I want to assign values to this class 20 times.
I fail with the message: NullReferenceException: Object reference not set to an instance of an object
If it were a GameObject, I would know
newPlayer = Instantiate(emptyPrefab, new Vector3(0,0,0), transform.rotation);
would work.
Here is what I have tried:
public DataClass[ ] myDataClass;
in void start:
myDataClass = new DataClass[20];
for (int i = 1; i < 20; i += 1)
{
myDataClass*.ID = i;*
}
And there is already the error message.
PLEASE can someone help?
for (int i = 1; i < 20; i += 1)
i should start at 0, because arrays in C# start at 0.
EDIT: it won’t let me post the code, hang on while I try and get it to work
Got it - it should be this:
myDataClass[i].ID = i;
Some notes on how to fix a NullReferenceException error in Unity3D
- also known as: Unassigned Reference Exception
- also known as: Missing Reference Exception
The basic steps outlined above are:
- Identify what is null
- Identify why it is null
- Fix that.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.