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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

@JSierraA$$anonymous$$A$$anonymous$$C'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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Resolutions 1 Answer

Editing buttons created in 'for' loop. 0 Answers

[Android] Why does my enemy die no matter where I am in the 3D environment? 1 Answer

Bool script error 1 Answer

getting !slot->GetLocalAABB().IsValid() and isFinite() errors with GUI Buttons 0 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