Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 danz_the_deadly · Jan 12, 2018 at 07:00 PM · uibuttonsetactive

How to hide non-active button?

Hello. I am making a game, where the main character can talk with different NPCs. When he comes close to them (enters trigger collider), "TALK" button appears. When he goes away, the button should disappear. I made it with button.SetActive(true/false).

 using UnityEngine;
 
 public class NPCInteraction : MonoBehaviour {
 
     public GameObject talkButton;
     
     void OnTriggerEnter2D (Collider2D other) {
         if (other.name == "Player") {
             talkButton.SetActive(true);
             Debug.Log(talkButton.activeSelf);
         }
     }
     void OnTriggerExit2D (Collider2D other) {
         if (other.name == "Player") {
             talkButton.SetActive(true);
             Debug.Log(talkButton.activeSelf);
         }
     }
 }

The problem is: button always remains on the screen.

Wat.

As you can see, I made public variable talkButton, so I can just drag any object (not just button) on it to test the script.

The button's active state changes correctly when I come close and go away, as I can see in debug window Debug.Log(talkButton.activeSelf);

alt text

Also the button's color changes to gray in hierarchy when it isn't active.

This is making me go completely bonkers when I try to understand whats going on. If I place another UI element (dialogue box for example) in the variable, it behaves exactly the same way (remains on the screen), but when I place entire UI (canvas), it works as intended (appears and disappears).

Later I wrote a new simple script to test this and put it into my canvas (UI):

 public class UIController : MonoBehaviour {
 
     public GameObject talkButton;
 
     void Update () {
         if (Input.GetKeyDown(KeyCode.Space)) {
             talkButton.SetActive(!talkButton.activeSelf);
         }
     }
 }

Exactly the same happens. Button remains on the screen. Same happens with any other UI element that I was testing except the entire canvas. Why do elements inside canvas remain there even deactivated? Do i have to make separate canvas for every element if I wanna toggle its state? I am going to get mad with this. HALP

screenshot-from-2018-01-12-21-39-53.png (12.2 kB)
Comment
Add comment · Show 2
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 getyour411 · Jan 14, 2018 at 06:34 AM 0
Share

In the first example, did you mean to do SetActive(false) on line 15?

avatar image danz_the_deadly · Jan 15, 2018 at 06:44 PM 0
Share

Yeah, sure

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Blaugrana · Jan 14, 2018 at 09:28 AM

try to change these things:

   using UnityEngine;
   public class NPCInteraction : MonoBehaviour {
  
      public Button talkButton;
      
      void OnTriggerEnter2D (Collider2D other) 
     {
          if (other.name == "Player") 
          {
              talkButton.gameObject.SetActive(true);
              Debug.Log(talkButton.activeSelf);
          }
      }
      void OnTriggerExit2D (Collider2D other) 
    {
          if (other.name == "Player") 
          {
              talkButton.gameObject.SetActive(false);
              Debug.Log(talkButton.activeSelf);
          }
      }
  }






Comment
Add comment · Show 1 · 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 danz_the_deadly · Jan 15, 2018 at 06:44 PM 0
Share

Why should it work?

avatar image
0

Answer by danz_the_deadly · Jan 15, 2018 at 07:18 PM

Actually I stopped making the button active/non-active. Instead I added canvasgroup to button and made it non-interactable, non-blockable and transparent instead of non-active.

It works well and fast, but it requires 3 times more code.

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

130 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

Related Questions

one button to hide and unihed gameobjects 0 Answers

Script Two Button Functions At Once 1 Answer

How can I set active a random Ui object from a list on a button press then reset it after the button pressed? 1 Answer

NullReferenceExeption on gameObject.SetActive 1 Answer

Graphics Raycaster - Click on overlapped buttons 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