So in my project, I have a class called ComponentRegistry. The idea of this class is to store a dictionary of components and their names. For example:
public Dictionary<string, MonoBehaviour> registry;
public MonoBehaviour testComponent;
void Start(){
registry = new Dictionary<string, MonoBehaviour> ();
registry.Add ("TestComponent", testComponent);
}
public MonoBehaviour GetScript(string name)
{
return registry[name];
}
Then in a separate class, I will access this dictionary using a string in order to retrieve the associated component, subsequently adding the associated component to the gameObject.
foreach (string comp in blockInfo.components) {
//For each of the components that need to be added
gameObject.AddComponent(ScriptMaster.compReg.GetScript(comp));
//I know this is incorrect, but the idea is there
}
Just for understanding, compReg is an instance of the ComponentRegister class stored as a static variable within my ScriptMaster class.
But no matter what I try, I have not been able to get this to work. I have switched endlessly between different formatting of adding components to gameObjects, alternating between using Components and MonoBehaviours, adding by type.
Thank you for any insight.