All the examples I’ve seen of Instantiate use an instance variable to contain the prefab that should be created. That variable is usually set manually in the Inspector:
Transform myprefab; //an instance variable which is set manually in the Inspector
public void CreateStuff() {
Transform mynewthing = (Transform)Instantiate(myprefab, ....
}
Is it possible to use the name of a prefab in some way to set this at runtime? Something like:
public void CreateStuff() {
Transform myprefab = FindPrefab("fatsoldier");
Transform mynewthing = (Transform)Instantiate(myprefab, ....
}
Can I load my own project as an AssetBundle and access prefabs in some way?
If not, how do you handle having a high number of prefabs which might be needed for a full game? Do you build a singleton that has a dictionary of name-to-prefab and keep that maintained in your Scene?
By way of explanation - I have an Infantry prefab that contains logic for the game. It can have a child GameObject which has an Animation component. I want to be able to load different Animation children depending on which type of infantry it is.
I’d rather not have duplicate Infantry prefabs where the only difference is the value of the Animation prefab variable. I’d like to be able to Instantiate a Infantry prefab and then call a method on it like
UseAnimation(“fatsoldier”,“rifle”);
Eventually those strings would be read from an XML file that has a roster of soldiers, their equipment and their attributes(speed,health,etc.)