- Home /
Whats wrong with my GUI.Toggle?
Hi Unity Community, This is a question with a simple solution I hope. I have a GUI Toggle as show below and I am trying to use the function like so:
public bool pressed = false;
//bool Toggle(Rect position, bool value, string text, GUIStyle style); //the template im using
pressed = GUILayout.Toggle(new Rect(10, 50, 50, 50), pressed, "Back*", "Button");
I am trying to make my toggle act as a button and position it, which is probably my problem... The toggle acting as a button works fine if I do not include the Rect(x,x,x,x) argument. Anyways Unity keeps giving me this error
"Assets/Scripts/SquadSelect.cs(151,37): error CS1502: The best overloaded method match for `UnityEngine.GUILayout.Toggle(bool, UnityEngine.Texture, params UnityEngine.GUILayoutOption[])' has some invalid arguments"
Which I dont understand because it isnt even the same format as what I am using. Does anyone know why this isnt working or a solution I can use? Thanks,
Answer by Eric5h5 · Sep 29, 2014 at 06:01 AM
See the docs for GUILayout.Toggle for the parameters you can use. Your code has a Rect, a bool, and two strings, which doesn't match anything that you can actually use. GUILayout functions typically don't use a Rect, to start with. If you want to use a Rect, you need to use GUI.Toggle.
@Eric5h5 the last string "Button" is actually supposed to be a style for the toggle. It is a toggle behaving as a button. I looked at the GUI.Toggle doc and it said I could do this. $$anonymous$$aybe I need to do: pressed = GUILayout.Toggle(new Rect(10, 50, 50, 50), pressed, "Back*", GUIStyle Button);
GUI.Toggle and GUILayout.Toggle are two quite different things.
Answer by Patel-Sagar · Sep 29, 2014 at 06:04 AM
You are using guilayout which is uses default layout in unity. If you want to position button at your specified place, Use GUI.toggle as below.
pressed = GUI.Toggle(new Rect(10, 50, 50, 50), pressed, "Back*", "Button");