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 siddharth3322 · Nov 25, 2020 at 06:20 PM · uigameobjectbuttonclick

On Button Click Enable - Disable GameObject Through Inspector

Through inspector on button click, how to enable and disable gameobject?

I have done this to enable and disable gameobject on button click but it is not working. alt text

This can only be able to enable the gameobject but it can't able to disable it. So how can I achieve this?

Code for CustomToggle script:

 public class CustomToggle : MonoBehaviour
 {
     bool isOn;
     //
     [SerializeField] GameObject onStateObj;
     [SerializeField] GameObject offStateObj;
 
     private void OnEnable()
     {
         isOn = false;
         onStateObj.SetActive(false);
         offStateObj.SetActive(true);
     }
 
     public void OnToggleButtonClick()
     {
         if (isOn)
         {
             // make this off
             isOn = false;
             onStateObj.SetActive(false);
             offStateObj.SetActive(true);
         }
         else
         {
             // make this on
             isOn = true;
             onStateObj.SetActive(true);
             offStateObj.SetActive(false);
         }
     }
 }

button-click-events.png (69.6 kB)
Comment
Add comment · Show 5
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 Llama_w_2Ls · Nov 25, 2020 at 06:29 PM 0
Share

We might need to see your custom toggle script to debug your implementation. Thanks.

avatar image siddharth3322 Llama_w_2Ls · Nov 25, 2020 at 06:37 PM 0
Share

I have updated the question with the code...

avatar image Llama_w_2Ls · Nov 26, 2020 at 07:20 AM 0
Share

It looks like it would work for me. One thing to note, I found that gameObject.SetActive(True) wouldnt work for me in some cases because it couldnt find the disabled gameObject in the scene. Try disabling/enabling the gameObject, or hiding its components to make it invisible. @siddharth3322

avatar image bobbaluba · Nov 26, 2020 at 08:16 AM 0
Share

Your script looks fine to me. Is the problem that your OnState gameObject remains active after the second click?

avatar image siddharth3322 bobbaluba · Nov 26, 2020 at 08:20 AM 0
Share

yes, gameobject always remains active because in inspector I have marked it in enable state. I want to make it like toggle behavior through the inspector rather than code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jimmiewalker653 · Nov 26, 2020 at 04:42 PM

 bool isOn;
 
 [SerializeField] GameObject onStateObj, offStateObj;
 
 public void OnToggleButtonClick()
 {
   isOn = !isOn;
   if (isOn)
   {
     onStateObj.SetActive(false);
     offStateObj.SetActive(true);
   }
   else
   {
     onStateObj.SetActive(true);
     offStateObj.SetActive(false);
   }
 }

Then remove your GameObject.SetActive on your button click and use only the OnToggleButtonClick and you should be good to go.

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 siddharth3322 · Nov 26, 2020 at 04:46 PM 0
Share

No, you are not getting the inspector image properly, the code enable and disable toggle images object. While in inspector, I want to enable and disable dialog object "SoundControllerDialog"

avatar image jimmiewalker653 siddharth3322 · Nov 26, 2020 at 04:50 PM 0
Share

Oh... ok... in that case you want to use [HideInInspector] above what you are trying to hide. If I understand what you are needing correctly.

avatar image siddharth3322 jimmiewalker653 · Nov 26, 2020 at 05:06 PM 0
Share

Again you not understand :)

I don't want to hide and show within the inspector, I want to enable and disable dialog object within the actual game, on button click event.

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

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

Related Questions

one button to hide and unihed gameobjects 0 Answers

How to wait click on button in my MessageBox analogue? 1 Answer

Incorrect button event positioning 0 Answers

How do I make a UI button to click a key? 1 Answer

Unity ads , can't we show video by clicking a button? 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