x


GUI.toggle always false

Hey guys I am new to unity and having a problem with a GUI.toggle. Basically all I want to do is check the value of the GUI.Toggle when I press a button. The problem im having is that the GUI.Toggle value is always false ? even when clicked ? Below is the code cheers to anyone who has insight into the problem

public class EditPersonalWorkoutScript : MonoBehaviour{ 
public bool checkValue;


 void OnGUI() 
{
    checkValue = GUI.Toggle(new Rect(120, 260, 130, 20), checkValue,"");

}


void OnMouseUp(){

addTheExercises();  

}



 private void addTheExercises(){

print (checkValue);
  }

  }
more ▼

asked Apr 20 '12 at 11:08 AM

grady-lad gravatar image

grady-lad
15 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You've declared the variable checkValue, but is toggling chestValue:

public bool checkValue;

void OnGUI() 
{
    chestValue = GUI.Toggle(new Rect(120, 260, 130, 20), chestValue,"");
}

NOTE: There's a missing left brace after MonoBehaviour:

public class EditPersonalWorkoutScript : MonoBehaviour { //<- this brace is missing

Is this a typo?

more ▼

answered Apr 20 '12 at 11:18 AM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

sorry them the two things you pointed out were typos I'll edit my question to fix these. But the GUI.Toggle is still returning a false value

Apr 20 '12 at 11:28 AM grady-lad

Well, this should work, unless you're declaring another checkValue inside the function, what would make it a temporary variable and hide the public checkValue. Another thing: public variables appear in the Inspector, which has elephant memory and will always start the game with the value set in the Editor - but the variable should toggle normally at runtime.

Apr 20 '12 at 12:47 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4151
x3681
x240

asked: Apr 20 '12 at 11:08 AM

Seen: 525 times

Last Updated: Apr 20 '12 at 12:47 PM