- Home /
Function in variable (UnityScript)
Hi, I need to call different function when buttons on my GUI are pressed.
var currentMenu;
function MainMenu(){
//Print Main Menu
}
function SettingsMenu(){
//Print Settings Menu
}
//Some More Menues
function Start(){
currentMenu = MainMenu;
}
function OnGUI(){
currentMenu();
}
Is there a way to do this? The code above doesn't work. Thanks for any suggestions!
Answer by boxburn · Jun 04, 2013 at 10:53 PM
Is this any use for you?
var MainMenu:boolean = true;
var SettingsMenu:boolean = false;
//var OtherMenus:boolean = false;
function OnGUI(){
if(MainMenu){
if(GUI.Button(Rect(Screen.width/2 - 150, 290, 300, 30), "Settings")){
MainMenu = false;
SettingsMenu = true;
// OtherMenus = false;
}
}
if(SettingsMenu){
//Print Settings Menu
}
if(OtherMenus){
//Print Other Menus
}
}
Thanks for this answer. That would be the alternative way I would have done it. Anyway, I'll wait for other answers, maybe it's possible to save functions in variables :)
I've never heard of saving a function as a variable, but you could just create a variable that links to a script. Or you could create a class? A class doesn't function, but it is a multi-array of sorts.
Your answer
Follow this Question
Related Questions
adding multiple values to single function/variable. 0 Answers
What is this C# code in javascript 0 Answers
Variable Doesn't Update in "Update" function 0 Answers
How can I storing the IEnumerator with parameters? 1 Answer
How do I reassign the result of a function back to the original arguments? 3 Answers