- Home /
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.
Well, you can just keep a reference to the last canvas activated.
public Canvas lastCanvas;
Somewhere along the line
lastCanvas = something;
That would do the trick.
Answer by JamesGryphon · Jun 16, 2017 at 05:22 PM
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 myCanvases = new Dictionary ();
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.
Answer by webcam · Jun 16, 2017 at 05:50 PM
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;
}
}
Well, the sample is a reduced version of the full script, for proof of concept. The actual main menu has something like eight different canvases, hence the need to keep track of what's going on.
Okay fair enough let's look at the dictionary then. What you want to do with a dictionary is create a string key and a corresponding canvas value. Something like this
public class $$anonymous$$ain$$anonymous$$enu : $$anonymous$$onoBehaviour
{
Dictionary menus = new Dictionary<string, Canvas>();
public Canvas $$anonymous$$enuCanvas;
public Canvas FirstCanvas;
public Canvas SecondCanvas;
public Canvas enabledCanvas;
void Awake()
{
menus.Add("$$anonymous$$enuCanvas", $$anonymous$$enuCanvas);
menus.Add("FirstCanvas", FirstCanvas);
menus.Add("SecondCanvas", SecondCanvas);
}
public void SetCanvasActive(string key)
{
enabledCanvas.enabled = false;
enabledCanvas = menus[key];
enabledCanvas.enabled = true;
}
}
Your answer
Follow this Question
Related Questions
string + int = variable 2 Answers
Text UI is not being assigned 2 Answers
Converting 'char' to the name of a variable 1 Answer
Change multiple variables with function argument 2 Answers
Public variable hidden in the inspector 2 Answers