- Home /
Select level script from a public property
I have a class called Level which inherits from MonoBehaviour and then multiple sub classes called Level1, Level2, Level3 etc.
My GameController class needs to instantiate the correct level. I'm not sure how to do this. I tried making a public property so that I could select it in the editor
public Level level;
void Start ()
{
level = gameObject.AddComponent<level>();
}
But this doesn't let me select the correct level script in the editor. (If I hard code the level script name into AddComponent then it works.)
I then tried using a string to use in AddComponent, but I can't work out how to do that either.
You can't use an "=" sign with AddComponent, AddComponent is an action you ask Unity to do, it's not returning any variable.
We need more explanations, what do you mean by "instantiate" the correct level ? Is that a level like a scene or like skill level for a character ? Do you want to call the good class from a script based on what skill level you character is ?