So the levels in the game I'm making are quite dynamic, and so I have almost no objects actually placed in the level (just a bit for tracking the rules, a camera and default light). Everything is instantiated right from the project library via scripts.
The problem I'm running into is: It seems like you can only instantiate a GameObject of Component from the library if it's referenced in another component. I mean this (C#):
class SomeScript : MonoBehavior {
public GameObject prefabToInstantiate;
void SomeMethod() {
Instantiate( prefabToInstantiate );
}
}
Here's the catch: The only way I've found to get an object to instantiate is to put this script onto another prefab, and go into the library and add the prefab I want to instantiate into the slot on the script.
So basically I'm forced to do one of two things: Either,
- Create a seperate prefab for every script that needs to instantiate, so that I can create the references on it, or
- Create a stupid "AllPrefabs" prefab and script, and basically duplicate my whole library as slots on that script so that other scripts can reference them.
This is exacerbated by the fact that you can't just create a prefab and put the script on it, oh no. You have to add an Empty game object in the level, and then create the prefab, and the add the Empty to the prefab, and then add the script to it, and THEN add the reference of the prefab that you are actually interested in.
So am I doing something wrong? Is there a way to just directly access a prefab from the library by name, or from a static class that the engine provides, or anything like that?
I am looking for something like this:
Instantiate( Library.GetPrefabByName("Enemy Crab") );