- Home /
Access clicked button from the different function
Hi, I already made
GUI.Button and GUI.Box
function on the different class and whenever I want to call the GUI, I just call the class and pass the parameter needed. Right now, the GUI are shown on the screen, but I could not think about how to make the GUI clicked. I mean, whenever I clicked the GUI, maybe something like
Debug.Log("You have clicked this button");
show on the Unity Console.
Here is the class code that I am using to store the GUI.Box and GUI.Button:
public class UserInformation{
public static GUIContent text, informationText, tooltipText; // For the content
public static GUIStyle style, informationStyle, tooltipStyle; // For the style
public static Rect rect, informationRect, tooltipRect; // For the position
public static void SetGUI(string content, int id, int minusWith)
{
// Switch with the parameter id
switch(id)
{
// If user selects 1 for the id, this case for the gold
case 1:
// Set the content, the style and the size of the box would be to the GUI.Box
//SetStyle(id);
// Set the GUIContent as the tooltip
text = new GUIContent(content);
// The GUILayoutUtility is useful because it is to fit the content
rect = GUILayoutUtility.GetRect(text, style);
// Set where the Rect have to be displayed
rect.x = Screen.width - rect.width;
rect.y = rect.height;
// Show the GUI as the box
GUI.Box(rect, text, style);
// Break it
break;
// If user selects 2 for the id, this case for the button
case 2:
// Set the content, the style and the size of the box would be to the GUI.Button
//SetStyle(id);
// Set the GUIContent as the tooltip
text = new GUIContent(content);
// The GUILayoutUtility is useful because it is to fit the content
rect = new Rect(0, 0, 150, 20);
// Set where the Rect have to be displayed
rect.x = 5;
rect.y = Screen.height - minusWith;
// Show the GUI as the button
GUI.Button(rect, text, style);
// Break it
break;
// If user selects 3 for the id, this case for the information
case 3:
// Set the content, the style and the size of the box would be to the GUI.Box
//SetStyle(id);
// Set the GUIContent as the tooltip
informationText = new GUIContent(content);
// Set the Rect
informationRect = new Rect(0, 0, 400, 225);
// Set where the Rect have to be displayed
informationRect.x = 0;
informationRect.y = (Screen.height / 10) - 50;
// Show the GUI as the box
GUI.Box(informationRect, informationText, informationStyle);
// Break it
break;
// If user selects 4 for the id, this case for the tooltip
case 4:
// Set the content, the style and the size of the box would be to the GUI.Box
//SetStyle(id);
// Set the GUIContent as the tooltip
tooltipText = new GUIContent(content);
// Set the Rect
tooltipRect = GUILayoutUtility.GetRect(tooltipText, tooltipStyle);
// Set where the Rect have to be displayed
tooltipRect.x = Screen.width - tooltipRect.width;
tooltipRect.y = Screen.height - tooltipRect.height;
// Show the GUI as the box
GUI.Box(tooltipRect, tooltipText, tooltipStyle);
// Break it
break;
// If user selects either 1, 2, 3 or 4 for the id
default:
// Show the error
Debug.LogError("You must choose between 1, 2, 3, or 4 only for the GUI!");
// Break it
break;
}
// End of switch
}
}
, but this time, whenever I want to check whether the button has been clicked by doing this code: (this is just an example of many GUI that has been shown on the screen)
if (UserInformation.SetGUI("Show Information", 2, Screen.height - 10))
it says that
cannot convert type void to bool
Basically, I want to check if the button have been clicked or not clicked yet.
Your answer much appreciated.
Thank you
Answer by Sisso · May 19, 2014 at 03:10 PM
You cannot convert a function that return nothing (void) into a boolean. SetGUI return nothing (void), so you can't use it in the if.
I really recommend you to start follow some unity3d/coding tutorials.
Your answer
Follow this Question
Related Questions
Set the position of GUIBox 1 Answer
Error CS1519 1 Answer
Not working gui buttons 1 Answer
Distribute terrain in zones 3 Answers
Font sizes, GUI, Iphones and even more head scracthing problems 0 Answers