x


Toggle button problem

 var ren : Renderer;   // renderer material
 var male : Texture2D;
 var female : Texture2D;
 var a1  = 0;
 var myskin : GUISkin;
 var togglemale : boolean = false;
 var togglefemale : boolean = true;


 function OnGUI () 
 {
   GUI.skin = myskin;
   togglemale= GUI.Toggle(Rect(Screen.width*(4.1/6.55),Screen.height*(    (4.7/6.3),Screen.width*(1.3/6.55),Screen.height*(.5/6.3)),togglemale,"MALE");
   togglefemale = GUI.Toggle(Rect(Screen.width*(4.1/6.55),Screen.height*(5.7/6.3),Screen.width*(1.3/6.55),Screen.height*(.5/6.3)),togglefemale,"FEMALE");

  if (togglemale== true)
  {
  ren.renderer.material.mainTexture = male;
  Debug.Log ("mmm");
   }

  if (togglefemale== true)
  {
  ren.renderer.material.mainTexture = female;
  Debug.Log ("fff");
  }


 }

i am new to unity my probelm is i am using a gui.toggle button named as MALE and FEMALE my need is when i select Male button male texture should display when i click FEMALE button female texture should display. But toggle button is not working properly .I have used renderer as cube

more ▼

asked Feb 02 '11 at 03:32 PM

robert 1 gravatar image

robert 1
23 9 10 14

any answer for this problem

Feb 02 '11 at 04:31 PM robert 1
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Design3.com is right, don't use 2 variables for the same inforamtion.

use this with the example code from Design3

var genderToggle : boolean = false;

genderToggle = GUI.Toggle(Rect([...]),genderToggle,"MALE");
genderToggle = !GUI.Toggle(Rect([...]),!genderToggle,"FEMALE");

or what would be better in such a case, use a ToolBar.

var gender : int = 0;
var toolbarStrings : String[] = ["MALE", "FEMALE"];

function OnGUI () {
   [...]
   gender = GUI.Toolbar(Rect ([...]), gender, toolbarStrings);
   if (gender == 0)
   {
      //Male
      [...]
}

To make a vertical toolbar use a SelectionGrid with xCount = 1

more ▼

answered Feb 07 '11 at 05:07 PM

Bunny83 gravatar image

Bunny83
46.9k 12 50 210

what is the use of !GUI IN this can u please explain i an new to unity your answer worked correctly thanks a lot

Feb 09 '11 at 06:44 PM robert 1

The ! is the logical NOT operator. That means when the Toggle function returns true the ! change it to false and when it returns false it gets true. That's nothing special in Unity that's basic programming and works in almost all languages that way. The ! is always the not-operator in all c-style languages like C,C++,C#,Java,Javascript, ... and much more. Here is a page that explain some operators in JS, but watchout, that page talk about JS in webpages. http://www.w3schools.com/JS/js_comparisons.asp

Feb 09 '11 at 07:05 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

I think you need to use one toggle instead of two. As it is, you have four potential combinations: male TRUE, female TRUE; male FALSE, female FALSE; male TRUE, female FALSE; and male FALSE, female TRUE.

Use one toggle ("genderToggle") and:

  if (genderToggle == true)
  {
  ren.renderer.material.mainTexture = male;
  Debug.Log ("mmm");
   } else {
  ren.renderer.material.mainTexture = female;
  Debug.Log("fff");
  }

Also your GUI code has some errors. It should be:

   togglemale= GUI.Toggle(Rect(Screen.width*(4.1/6.55),Screen.height*(    (4.7/6.3),Screen.width*(1.3/6.55),Screen.height*(.5/6.3)),togglemale,"MALE");
   togglefemale = GUI.Toggle(Rect(Screen.width*(4.1/6.55),Screen.height*(5.7/6.3),Screen.width*(1.3/6.55),Screen.height*(.5/6.3)),togglefemale,"FEMALE");

By using "toggleMale" instead of "togglemale", you declared a new variable instead of referring to the variable you declared at the top of the script.

more ▼

answered Feb 02 '11 at 09:29 PM

Design3.com gravatar image

Design3.com
655 1 1 11

(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:

x820
x699
x169

asked: Feb 02 '11 at 03:32 PM

Seen: 2005 times

Last Updated: Feb 02 '11 at 10:11 PM