Change GUI Style on Button Press

I’m working on setting up a GUI in the style of the Sierra VGA point-and-click games. I’ve got many of the functions working to where I can switch between them, however for one function I want to be able to change the STYLE of a particular toggle whenever a different button is pressed. I have the styles defined in the GUI Skin, and the individual styles THEMSELVES work, the problem is I can’t figure out how to switch between them.

In other words:

Toggle 1 has X different styles I want to switch between
There are X different buttons. Clicking each one is to set a particular Style for Toggle 1

I’ve posted this on Unity Answers and I THINK I’m part way there using the feedback I’ve gotten, I’m just missing SOMETHING. I’ve tried a number of different ways to code this (using Toggles, Buttons, various ways of calling the new Style, etc) and tried following the recommendations given, but I just can’t figure out how to actually do it.

I’m writing this in Javascript. I don’t even need the specific code written out (though that would help) but enough of it to see what I should be doing.

Well use:

GUIStyle shizzle = new GUIStyle();

GUI.Toggle(rect, text, shizzle);

So if you need to reskin the toggle, just switch the shizzle to another style.

You can choose one of your skins styles with:

if(GUI.Button(rect,text)) {
shizzle = GUI.skin.GetStyle(“toggle style one”);
}

I tried it with one of my secondary functions but something isn’t right. Here’s the relevant snippet of code:

//Toggles Move
	if (GUI.Toggle (Rect (140,49,18,57), toggleMove, "", "MoveWalk")) toggleMove = setMeOnly();
		if (toggleMove  (action_state != "move"))
			{
  			action_state = "move";
  			}
	//Sets Move to Walk
		if (GUI.Button (Rect (145,18,11,28), "", "WalkButton")) toggleMove = setMeOnly();
			if (toggleMove  (move_state != "walk"))
				{
  				move_state = "walk";
  				toggleMove = GUI.skin.GetStyle("MoveWalk");
  				toggleMove = true;
 				}

The style I’m calling is already defined within the skin itself so I didn’t define it elsewhere within the script. This generates an error: BCE0022: Cannot convert ‘UnityEngine.GUIStyle’ to ‘boolean’.

toggleMove = GUI.skin.GetStyle(“MoveWalk”);
toggleMove = true;

That fires the error.

I suppose you wanted to write:

move_state = GUI.skin.GetStyle(“MoveWalk”);
toggleMove = true;

Move_state is what I’m using to tell my control script how the character moves. The character control script will reference this script to see what the currently selected action_state is. If action_state = “move”, it then checks the move_state and modifies the character’s movement speed, animation, and other factors accordingly.

I can’t use move_state to set the style of the Move toggle, otherwise it breaks how the control script will determine the character’s movement type. Additionally, that also doesn’t seem to tell the script on what function to APPLY the new style. When the Walk button is pressed I need it to change the style of the Move TOGGLE.

Sorry didin’t read too carefully.

Anyways:

It doesn’t like that either. Errors:

BCE0043: Unexpected token: GUIStyle comes up on private GUIStyle

UCE0001: ‘;’ expected on Start()

BCE0044: expecting :, found ‘=’ on

Start()
{
style = GUI.skin.GetStyle("MoveWalk");
}

This is example code.

Methods ( Start, OnGui ) must have a return type. That means you have to state what the method returns, in this case nothing => return type void. ( void Start() {} ) (void OnGUI() {}).

UCE0001 is because you tried writing the example method I wrote in Another! method, when doing that you are trying to CALL the method Start(), not initialize it.

So it should be more like:

void Start() {
style = GUI.skin.GetStyle("MoveWalk");
}

What about BCE0044? Is that caused by the same as BCE0001?

I can’t tell you about that. I will need more of your code.

This is the relevant code as it currently exists:

function setMeOnly():boolean
	{
	toggleMove = toggleInteract = toggleLook = toggleTalk = toggleInventory = false;
	return true;
	}

private GUIStyle move;

Start()
	{
	move = GUI.skin.GetStyle("MoveWalk");
	}

Update()
	{
		{
		if (move_state = "walk")
		move = GUI.skin.GetStyle("MoveWalk");
		}
		else
		{
		if (move_state = "run")
		move = GUI.skin.GetStyle("MoveRun");
		}
		else
		{
		if (move_state = "stealth")
		move = GUI.skin.GetStyle("MoveStealth");
		}
	}

function OnGUI()

{

//Display resolution stuff goes in this line here, I've removed it as it's irrelevant to the issue

//Toggles Move
	if (GUI.Toggle (Rect (140,49,18,57), toggleMove, "", move)) toggleMove = setMeOnly();
		if (toggleMove  (action_state != "move"))
			{
  			action_state = "move";
  			}
	//Sets Move to Walk
		if (GUI.Button (Rect (145,18,11,28), "", "WalkButton")) toggleMove = setMeOnly();
			if (toggleMove  (move_state != "walk"))
				{
  				move_state = "walk";
  				toggleMove = true;
 				}
	//Sets Move to Run
		if (GUI.Button (Rect (181,38,19,28), "", "RunButton")) toggleMove = setMeOnly();
			if (toggleMove  (move_state != "run"))
				{
  				move_state = "run";
  				toggleMove = true;
				}
	//Sets Move to Stealth
		if (GUI.Button (Rect (101,38,19,28), "", "StealthButton")) toggleMove = setMeOnly();
			if (toggleMove  (move_state != "stealth"))
				{
  				move_state = "stealth";
  				toggleMove = true;
				}

I’ve removed all the other toggles from this snippet, which are irrelevant to this. I can switch between the various control toggles just fine so that’s not an issue. I’ve also cropped out the different variables at the start.

I have NOT made any of the changes as you outlined in your last post (IE, to Start () ). I wanted to show you what I had as it currently exists.

Ooh you’re using Javascript. This is the line I had to read…

the return types in javascript are written as follows :
function Start() : void {

}

And variables are a bit different than I said :
private GUIStyle style, becomes :
var style : GUIStyle;

Sorry for misleading you, I’m not in my best today :smile:

Ah, Ok. Yeah, I THOUGHT private was a function of C#. Anyway, I’m getting somewhere now. Still getting a few errors before I can test to see if the style actually changes:

This line:

if (move_state = "walk")

Now generates the following errors:

BCE0044: expecting ), found ‘=’.
BCE0043: Unexpected token: walk.

and

move = GUI.skin.GetStyle("MoveWalk");

generates BCE0044: expecting :, found ‘=’.

if (move_state = “walk”) parsed by the computer means :
If ( assign value “walk” to move_state ) which is an error.

“=” is used for assigning, while “==” is used for comparing.

About the second one - did you change :
private GUIStyle move;
to :
var move : GUIStyle;

Ok, so I set the Start and Update functions like this:

function Start() : void
	{
	move = GUI.skin.GetStyle("MoveWalk");
        toggleMove = true;
	action_state = "move";
	move_state = "walk";
	}

function Update()
	{	
	if(move_state == "walk")
		{
		move = GUI.skin.GetStyle("MoveWalk");
		}
	else(move_state == "run")
		{
		move = GUI.skin.GetStyle("MoveRun");
		}
	else(move_state == "stealth")
		{
		move = GUI.skin.GetStyle("MoveStealth");
		}
	}

It gives me UCE0001: ‘;’ expected Insert a semicolon at the end. on the else lines, and BCE0044: expecting :, found ‘=’ on the lines specifying move =.

Changing the Else statements to If removes these errors and the game will launch in Unity. However running the game then generated an in-game error that GUI styles cannot not be called outside OnGUI.

I then tried moving move = GUI.skin.GetStyle("TheStyles");

to list them under the actual Toggle for movement and deleted GUI.skin.GetStyle from the Start function. This eliminated all errors and got one of the images to display, but it’s still not actually changing the style when I click another function.

I then tried moving this code into the statements for their respective buttons like this:

//Sets Move to Walk
		if (GUI.Button (Rect (145,18,11,28), "", "WalkButton")) toggleMove = setMeOnly();
			if (toggleMove  (move_state != "walk"))
				{
  				move_state = "walk";
                                move = GUI.skin.GetStyle("MoveWalk");
  				toggleMove = true;
 				}

Once again, move = IS picking up one of the specified styles, it’s just not switching between them when clicking on another button.

It’s ALMOST there.

When having errors like that, the first thing to do is to add some Debug.Log()-s to see if you actually enter the body of the function.

Like that:

If it prints try adding Debug.Log in another if() statements to check if the program enters them at all.

Yeah, the Debug prints when I put it under any of the three buttons. I also knew the buttons themselves were working because they are activating toggleMove as I set them up to do. The only problem is that it’s not changing the style OF the toggle.

Are you sure that the styles acutally differ from each other :smile:

Yep, they are.

Incidentally, when I remove the last of the three style change options from the script, it loads the the next to last style change. It still doesn’t switch between the two. So it appears that the style change IS in effect somehow, the problem is that it’s not selecting the style based on which button is pressed but defaulting to whatever the last style mentioned in the script is.

I wonder if I might need to make the buttons that change the style into Toggles as well, instead, that way the associated style will be turned On and Off when selected…

Tried to make them a Toggle instead of a button, but it didn’t work.