- Home /
Reset and arrays.
When I click Reset in Inspector the array is created/resized with 4 elements. But I can't modify its values. I get the error: NullReferenceException: Object reference not set to an instance of an object
What am I doing wrong?
[System.Serializable]
public class Product{
public string nombreID;
}
public class Shop : MonoBehaviour {
public Product[] consumibles;
public void Reset(){
consumibles = new Product[4];
consumibles[0].nombreID = "coins_1000";
}
}
Answer by QuiZr · Jul 17, 2016 at 11:40 PM
The array is initialized but objects in it aren't. You have "pointers to objects" but those objects doesn't exist. After you've initialized your array you need to initialize a new Product in that place:
consumibles[0] = new Product();
Your answer
Follow this Question
Related Questions
Inspector variables from class in List are always initialized to its default values 1 Answer
Multidimensional Arrays of Classes in Java 2 Answers
Declare new class[].vector3[] length? 2 Answers
"Null Reference Error" when using a custom class as an array 1 Answer
Accessing variables and arrays from inside classes? 1 Answer