Class arrays

Hello i got a simple public class ,
but I don’t understand how can I add multiple objects as an array of that class.

As it’s public, in the editor I’ve set the size of the entities array, but I still can’t create objects.
I’m missing the obvious :confused:

    public class entity{
        public string name;
       ..
    }
    public entity[] entities;
entities[0].name="foobar"; //returns error "NullReferenceException: Object reference not set to an instance of an object"

you haven’t instantiated anything into the [0] slot.

entities[0] = new entity();

edit: you’ve also not created or specified the size of the array, only declared it

public entity[] entities = new entity[10]; // 10 slots
2 Likes

In the unity editor, I see in the inspector the Entities , and i set the value to 5.
Sot it equals to public entity[ ] entities = new entity[5];

But I thought that declare it in the inspector implicitly instantiate it :confused:
so I needed to instantiate it first,
thanks again