- Home /
Changing one button style's background changes for all styles
In the code below I am trying to have different styles for different buttons. The handStyle GUIStyle is used for cards that should be shown and blankHandStyle for cards that should give the appearance of being upside down. When I remove the last line all buttons show the handStyle GUIStyle, when it is there all buttons show the cardBack texture. When creating buttons in the OnGUI function I have correctly assigned Styles in the parameters.
//Hand
handStyle = GUI.skin.GetStyle("button");
handStyle.normal.background = normalButton;
handStyle.fontSize = Mathf.RoundToInt(((Screen.width/Screen.height)*11.5f)*GUIScale);
blankHandStyle = GUI.skin.GetStyle("button");
blankHandStyle.normal.background = cardBack as Texture2D;
Answer by shriya · Dec 23, 2014 at 08:10 AM
Hi,
Problem here is that you are passing button style to both handStyle and blankhandstyle which is why changing the background of one changes for all. You need to create two different custom styles and and pass them to hand style and blankHandStyle separatly ,then it would not affect each other. Hope you are clear what i mean.
I had forgot to come back and amend this, I apologize, this was the answer. I wasn't aware that by using the GetStyle function that it meant when you changed it in a variable it would change the default style as well. Thank you.
Answer by salvador007 · Dec 23, 2014 at 10:17 AM
Here you need to pass different styles to both the buttons. You can achieve this by creating a GUISKIN and then making custom styles as much as you want.
You can take help from below:
Now in Code assign this GUISKIN publically.
C# code:
public GUISkin mainMenuSkin;
and then
handStyle = mainMenuSkin.GetStyle("CustomStyle1");
blankHandStyle = mainMenuSkin.GetStyle("CustomStyle2");
so on..
Your answer
Follow this Question
Related Questions
GUI button doesn't appear on Android 0 Answers
How can I get the Unity 3 texture for 'button on'? 2 Answers
Specify OnMouseEnter 2 Answers
Making texture cover whole button 1 Answer
DrawTexture GUI iPhone 1 Answer