• 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
Question by soulpiper12 · Nov 15, 2014 at 03:10 AM · guibutton

Button Turns Off and On Object

I've successfully created a code to turn off an object when a button is pressed. That being said I want to be able to press the button again to turn the object back on. (possible even to change color to indicate when it is on or off)

Unfortunately when I press the button the object disappears (which is good) but the button disappears too (which is bad) . what can I add to my script to accomplish the above. Thanks for your help :) using UnityEngine; using System.Collections;

 public class ButtonGuiScript : MonoBehaviour {
 
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
     
     }
     private void OnGUI()
     {
         if(GUI.Button(new Rect(15, 15, 100, 50), "West Wind Zone"))
         {
             gameObject.SetActive(false);
         }
 
         
         
     }
 
 }
 
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by NoseKills · Nov 15, 2014 at 11:18 AM

or just

 cube.SetActive(!cube.gameObject.activeSelf);

depending on whether you prefer shorter code or less variables

Comment
Kiwasi

People who like this

1 Show 0 · 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

Answer by Kiwasi · Nov 15, 2014 at 03:13 AM

Standard way to do this is

 bool isOn;

 private void OnGUI(){
     if(GUI.Button(new Rect(15, 15, 100, 50), "West Wind Zone")){
          isOn = !isOn;
          gameObject.SetActive(isOn);
     }    
 }
Comment

People who like this

0 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 soulpiper12 · Nov 15, 2014 at 03:37 AM 0
Share

Thanks for the response! I think my mistake is that I've attached the script directly to the game object that I want to vanish. When it vanishes the button disappears as well. How can I create a button to refer to the cube without being a script attached to the cube itself?

I'm reasonably new to unity and my scripting skills are rough. Would I write a script and attach it to another object and reference the cube this way?

 var cube:"TameObject" ##name of the gameObject I'm referencing
avatar image JSierraAKAMC · Nov 15, 2014 at 04:55 AM 1
Share

That's close. It would be something like:

 public GameObject cube;
 private bool active;
 
 private void OnGUI(){
      if(GUI.Button(new Rect(15, 15, 100, 50), "West Wind Zone")){
           active = !active;
           cube.SetActive(active);
      }
 }
 

And then you'd attach it to some object that will stay active, such as the main camera. After that, just drag in the cube from the Hierarchy to the Inspector to the "Cube" slot.

avatar image Kiwasi · Nov 15, 2014 at 05:31 AM 0
Share

@JSierraAKAMC's code will do the trick.

@soulpiper12 You've used the JavaScript syntax in your comments, where your question was in C#. Trying to mix the two will only cause trouble. Note that var is generally a JavaScript term. It means something different in C# and is seldom used.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUI Button not working...but the the script is correct... 2 Answers

(4.6 UI) How to set up a window with x buttons, with a scroll bar? 1 Answer

Null reference exception[SOLVED] 1 Answer

how can i make my GUI disapear after i pressed a button? 2 Answers

GUI.button order 1 Answer


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