- Home /
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
and
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?
Answer by whydoidoit · Apr 23, 2013 at 06:12 AM
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.
right but what about this playing coherently with Instantiate()
? say (for a Node prefab):
Instantiate(Resources.Load("Node"), transform.position, Quaternion.identity)
returns a generic Object
, which is not even the script type. What I'm wondering is if for a Node
prefab with a Node$$anonymous$$echanics
script attached, there is a way for me to statically type a list or array of only those prefab types. For your suggestion to work, I believe I'd have to still cast the Object
to GameObject
after Instantiate()
and then further do a GetComponent()
and then assign that component to the list / array. Further, the approach begs the question: should I even think of prefab names as anything other than a convenience identifier? $$anonymous$$ight as well rename the script to Node
ins$$anonymous$$d of Node$$anonymous$$echanics
and think of it as representing the type of the prefab?
You cannot cast a GameObject to a script this is a composition model - you have to call GetComponent< YourScript>();
If you make that array of prefabs then instantiating them will return your script. You didn't mention you are using Resources.Load, which in fact should only be used in rare circumstances - you'd be better off having an object in the scene that contains your prefabs. Resources cause lots of challenges when making bigger games - like making it hard to create Asset Bundles that are efficient etc.
If you want to continue to use Resources.Load then you would need to make your line:
prefabs.Add( (Resources.Load("Node", typeof(GameObject)), transform.position, Quaternion.identity) as GameObject).GetComponent<YourScript>());
If you have a scene object that contains a list, or multiple types of YourScript variables then:
prefabs.Add(Instantiate(yourPrefabVariable, transform.position, Quaternion.identity) as YourScript);
Because the result of the instantiate will be YourScript but it's typed as UnityEngine.Object
yeah it returns Object anyway. what DOES work though...
Instantiate(Resources.Load("Node", typeof(Node$$anonymous$$echanics)), transform.position, Quaternion.identity) as Node$$anonymous$$echanics;
Really consider not using Resources - they're a total disaster in my book. I just took delivery of a component for my game from a third party and my build increased by 100$$anonymous$$B because they'd put everything in a resources folder (including a picture of one guy's wife, a bunch of stuff they once used but upgraded etc - go figure) even though they only loaded 2 components from it at runtime.