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.
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’.
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.
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.
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.
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.
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.
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.
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…