how can I create an array or IList of a specific prefab type in C# ?

So, in theory all prefabs are of type GameObject. So how would I go about creating an array or IList that can only hold instances of specific prefabs?

That is, say I have a Node prefab with geometry and a NodeMechanics script attached.

I want Node prefabs to have (in the script) the properties

  • IListpreviousNodes


  • IListnextNodes

So that I can attach more node prefabs. If the lists where of type GameObject, any GameObject could be inserted, which I don’t want.

I also thought of going for a Node script instead of NodeMechanics, so that the script itself, being a class, goes on to act as the “type” of the prefab. Though I think this is kind of against the Unity semantics or at least idiom, where a script seems to be thought of as one more component of the prefab?

You can just use List< YourScript>;

 List<YourScript> prefabs = new List<YourScript>();

When you instantiate you will need to cast the result to YourScript.

Prefabs are GameObjects, not a thing derived from them because this is a composition model not an inheritance one.