- Home /
After Adding GUIStyle to GUI.Button, it is no longer clickable
I want to make a button out of the font that I have. I set up the javascript variable
var comicStyle : GUIStyle;
(and in the inspector I've edited the font, font size, normal color, and Alignment, everything else is default)
if I use this line of code:
if(GUI.Button(Rect(200, 200, 300, 75), "feedback", comicStyle))
{
print("You Clicked the Feedback button");
}
it draws the words "feedback" on the screen right where I want it, but I can't click it.
if I remove the style argument from the GUI.Button, like this...
if(GUI.Button(Rect(200, 200, 300, 75), "feedback"))
{
print("You Clicked the Feedback button");
}
it makes a gray button rectangle with the word "feedback" in it, that I can click, and see the print command execute in the console.
My question is, is there something I'm not setting correctly in my style that is allowing this to be "click-able"? or am I totally off?
I can easily make a sprite out of the word, but I'm looking to reduce the amount of images in my game with concerns of file size.
Thanks in advance!!!
nope, the "IF" statement it in the OnGUI() function, like so.
function OnGUI()
{
if(GUI.Button(Rect(200, 200, 300, 75), "feedback"))
{
print("You Clicked the Feedback button");
}
}
it works if I simply do not include the "style" which makes me think the style is making it no longer function as a button :/ But I can't find any info about that. There is some about GUISkin, but i can't seem to find a way that it makes a difference.
Answer by Bunny83 · Jul 23, 2014 at 09:27 PM
Well, you shouldn't declare a GUIStyle that way. It's better to create a GUISkin and use that one. In the skin asset you could edit the Button style or add a custom style at the bottom and use that one. To use a GUISkin you just declare a public variable of type GUISkin and inside OnGUI you set your skin as the current. Something like that:
var mySkin : GUISkin;
function OnGUI()
{
GUI.skin = mySkin;
// Your GUI stuff
}
Note: If you use a custom style and your skin is "active" you can simply use the style name as string, like so:
GUI.skin = mySkin;
if(GUI.Button(Rect(200, 200, 300, 75), "feedback", "feedbackStyleName"))
{
You can also use any other style like this. This would draw a button with the label style:
if(GUI.Button(Rect(200, 200, 300, 75), "feedback", "label"))
{
on thing that also seemed to work, is that the values "300" for the GUI element X width, was smaller than the Styles Font size. So there actually WAS a button there, i just couldn't see it because the Style had no background image set up. Once i through a stand in square texture, I was able to see the "hit box" for the button, and adjust my code to make it fit the text.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUIStyle not working on GUI.Button 4 Answers
click on GUI button goes through to a collider... 0 Answers
Change GUI.Box color when click button 1 Answer
GUI.Button press (not click) 3 Answers
Stop clicking through a GUI window 2 Answers