Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 earar · Jan 25, 2013 at 11:38 AM · guigameobjectbuttonhide

how to toggle on/off a gameobject with a GUI button ?

Hello,

I would like to be able to toggle on and off an object by clicking on a button.

I would like it to be generic, so I can assign it to any object I want and when I click on one button, I hide the objet and when I click on another button, I make it visible again.

What kind of function do I need to create to toggle on and off an object ?

I would like to use javascript for it and I just have the basic code :

 // JavaScript
 
 var icon : Texture2D;
 var toggleBool = true;
 var target : GameObject;
 var target2 : GameObject;
 
 
 function OnGUI () {
     GUI.Box (Rect (0,0,100,90), "Top-left");
     
     if (GUI.Button (Rect (10,30,80,20), "turn on")) {
         
     }
     
     if (GUI.Button (Rect (10,60,80,20), "turn off")) {
         
     }
 }

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jan 25, 2013 at 11:48 AM

If you just want to make the object invisible, set renderer.enabled to false. If you want to deactivate the object, use gameObject.SetActive(false). Your code could be something like this:

 var icon : Texture2D;
 var toggleBool = true;
 var target : GameObject;
 var target2 : GameObject;
 
 
 function OnGUI () {
     GUI.Box (Rect (0,0,100,90), "Top-left");
 
     if (GUI.Button (Rect (10,30,80,20), "turn on")) {
       target.SetActive(true);
     }
 
     if (GUI.Button (Rect (10,60,80,20), "turn off")) {
       target.SetActive(false);
     }
 }

You could also use a single Toggle button instead of two buttons:

 var icon : Texture2D;
 var toggleBool = true;
 var target : GameObject;
     
 function OnGUI () {
     GUI.Box (Rect (0,0,100,90), "Top-left");
     toggleBool = GUI.Button (Rect (10,30,90,20), toggleBool, "turn on/off");
     target.SetActive(toggleBool);
     // or target.renderer.enabled = toggleBool; for just making it visible or not
 }
Comment
Add comment · Show 3 · 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 Eowyn27 · Feb 13, 2014 at 02:09 PM 0
Share

Cool, thanks. I was wondering what if I wanted to make the object turn red on toggle?

avatar image Kamhal · Feb 13, 2014 at 07:41 PM 0
Share

You can change the renderer material color.

 target.renderer.material.color = Color.red;
avatar image Eowyn27 · Feb 14, 2014 at 02:03 PM 0
Share

That theoretically works but I'm running into this problem: http://answers.unity3d.com/questions/639169/changing-gameobject-material-color-makes-object-di.html

avatar image
0

Answer by earar · Jan 25, 2013 at 01:17 PM

merci

j'avais déjà essayé avec l'active mais j'ai dû ma planter qqpart, ca buggait.

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
avatar image
0

Answer by Siusega · Apr 22, 2013 at 03:19 AM

Thanks for this thread, script works fine!.

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.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detect Click on Gameobject 0 Answers

How do I make individual buttons change individual variables? 0 Answers

Cube click pop-up menu 0 Answers

Using GUI and check what button was pressed 1 Answer

GameObject touchable instead GUI button 3 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