• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by robert 1 · Feb 02, 2011 at 03:32 PM · buttontoggle

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robert 1 · Feb 02, 2011 at 04:31 PM 0
Share

any answer for this problem

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Feb 07, 2011 at 05:07 PM

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robert 1 · Feb 09, 2011 at 06:44 PM 0
Share

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

avatar image Bunny83 · Feb 09, 2011 at 07:05 PM 0
Share

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 program$$anonymous$$g 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

avatar image
0

Answer by Design3.com · Feb 02, 2011 at 09:29 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is it a bad idea to add and remove listeners for toggles and buttons at runtime? 2 Answers

Button and resolution problem 1 Answer

How to realize action on button click with script? 1 Answer

GUISkin & Scripting 2 Answers

Check GUI toggle button with the M keyboar Key 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges