I have made a menu scene for a Unity application, where the user selects a training scenario: 1 of 4 available options in the form of a toggle group:
public enum trainingScenarios
{
None = 0,
scenario_1,
scenario_2,
scenario_3,
scenario_4
}
So, once the user ticks a check-mark, the selected scenario becomes that:
public void confirmScenario(int index)
{
switch (index)
{
case 1:
selectedScenario = trainingScenarios.scenario_1;
break;
case 2:
selectedScenario = trainingScenarios.scenario_2;
break;
case 3:
selectedScenario = trainingScenarios.scenario_3;
break;
case 4:
selectedScenario = trainingScenarios.scenario_4;
break;
}
}
Now... In the next scene, I wanna use the selectedScenario
variable to display a message that this is the scenario being applied. Currently, I get Unity to not destroy my menu game object, so I can get a reference to the object and the script, etc... just to use this variable. Makes sense, because so far it is the only time I need to use the selectedScenario
variable in a different scene.
However, now I need this selectedScenario
variable again to determine what kind of animation to apply to a character, say walking for scenario 1 and running for scenario 2 and etc...
So, again, I need to do the following just to gain access to the selectedScenario
variable that was set in the initial menu scene, and worse still, since I do this in the Start
method of a scene, I can only use it there immediately, and not in other methods...
GameObject menuManager = GameObject.Find("Menu Manager");
MenuScript menuScript = menuManager.GetComponent<MenuScript>();
Debug.Log("Selected Training Scenario: " + menuScript.selectedScenario);
Is this a good way to get a reference to this variable when you need to, or is this perhaps a good candidate for a singleton design?
I am a novice Unity and C# programmer, so I am trying to get my head around when to use this design pattern. I would appreciate kind and informative comments to help me re-design this little thing.