Using a String in an Enable statement

I’m working on a main menu that has a number of buttons that the user can press at any time, switching from one panel to another. I thought it’d save time and some speed, as well as be much more elegant, to keep track of which button the user most recently pressed, and then simply turn that one off when switching to a different panel, rather than disabling all of them every time. So I wrote this:

public class MainMenu : MonoBehaviour {
	public Canvas MenuCanvas;
	public Canvas FirstCanvas;
	public Canvas StartCanvas;
	public Canvas JoinCanvas;
	public Canvas PrefsCanvas;
	public Canvas GuideCanvas;
	public Canvas StatsCanvas;
	public Canvas MusicCanvas;
	public Canvas SoundCanvas;
	string CanvasName; 

	void Awake()
	{
		MenuCanvas.enabled = true;
		FirstCanvas.enabled = true;
		CanvasName = "FirstCanvas";
	}

	public void JoinOn()
	{
		CanvasName.enabled = false;
		JoinCanvas.enabled = true;
		CanvasName = ("JoinCanvas");
}

The problem is, I can’t figure out how to get CanvasName to return its value, and to use that as part of the enable statement. It’s obvious why CanvasName.enabled won’t work; it’s trying to turn off the string itself and not reacting to the value of that string, but knowing that doesn’t help me much.

If anyone has any ideas about how to make this work, or better suggestions for how to implement this kind of activity tracking, I’d be grateful.

I worked out a solution based on some other people’s posts, using the dictionary method, and thought I’d include a sample to help anyone else that stumbles across this out. I haven’t tried rev’s proposal, but readers are welcome to give it a go for themselves and see how it works if they don’t want to go to this much trouble.

public class Sample : MonoBehaviour {

public Canvas FirstCanvas;

public Canvas JoinCanvas;

Dictionary<int,Canvas> myCanvases = new Dictionary<int,Canvas>();

void Awake()
{

FirstCanvas.enabled = true;

myCanvases[0] = FirstCanvas;

}

public void JoinOn()
{

myCanvases [0].enabled = false;

JoinCanvas.enabled = true;

myCanvases[0] = JoinCanvas;

}

}

I don’t promise that it’s the easiest or best solution, but it is definitely workable; I have it running in my own main menu.

You can accomplish this same result without the dictionary here. Currently, you have references to the two canvas you want to enable and disable and you can just use those. If you want to use a string as a key a dictionary makes sense but the way you have it working now you can just simplify it to use your references.

public class Sample : MonoBehaviour {

public Canvas FirstCanvas;

public Canvas JoinCanvas;

 void Awake()

{  

     FirstCanvas.enabled = true;

}

 public void JoinOn()

 {
     FirstCanvas.enabled = false; 

     JoinCanvas.enabled = true;

 }

}