• 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 NotEth4n · May 22, 2019 at 03:40 AM · uigameobjectbuttondestroyclickable

Buttons still visible after Destroy() but say "deleted gameobject" in hierarchy

Using Unity 2018.3.11f. I am trying to show a pickup button when the player walks over an item, or several, and then removes the button if the player walks to far away. This works great, except the actual button only works sometimes. In Game View, the UI Buttons are added and removed at the correct distances. When going to click on the Buttons, however, sometimes the Buttons are clickable and work great, other times the Buttons don't even show up as highlighted when hovered over. When checking the scene view, the UI Button GameObjects are correctly located as children of the Canvas, but when clicked in the Hierarchy, nothing shows up. I managed to see it say "deleted gameobject" in place of the Button name when I clicked one of the buttons once, which flashed back to the original name. This happens both in editor Play Mode and in a standalone build. Here is the code I am using for creating and destroying the Buttons:

 // Update is called once per frame
     void Update()
     {
         for(int i=nearbyInteractables.Count-1;i>=0;i--)
         {
             if (nearbyInteractables[i].interactable != null)
             {
                 if (Vector2.Distance(nearbyInteractables[i].interactable.transform.position,transform.position) > interactionRange)
                 {
                     Destroy(nearbyInteractables[i].itemInteractionButton.gameObject);
                     nearbyInteractables.RemoveAt(i);
                 }
             }
             else
             {
                 Destroy(nearbyInteractables[i].itemInteractionButton.gameObject);
                 nearbyInteractables.RemoveAt(i);
             }
         }
 
         Collider2D[] nearbyColliders = Physics2D.OverlapCircleAll(transform.position, interactionRange, interactableLayers);
         foreach (Collider2D collider in nearbyColliders)
         {
             ClientInteractable clientInteractable = collider.GetComponentInParent<ClientInteractable>();
             if (clientInteractable != null)
             {
                 if (clientInteractable.enabled == true)
                 {
                     if (nearbyInteractables.Any(i => i.interactable == clientInteractable) == false)
                     {
                         ItemInteractionButton itemInteractionButton = uiManager.interactionDisplay.AddItemButton(clientInteractable);
                         itemInteractionButton.button.onClick.AddListener(delegate
                         {
                             Interact(clientInteractable);
                         });
                         nearbyInteractables.Add(new NearbyInteractable(clientInteractable, itemInteractionButton));
                     }
                 }
             }
         }
         uiManager.interactionDisplay.ShowItemDisplay(nearbyInteractables.Count > 0);
     }
 
 private void Interact(ClientInteractable clientInteractable)
     {
         if (clientInteractable == null) return;
         LinkedEntityComponent linkedEntityComponent = clientInteractable.GetComponentInParent<LinkedEntityComponent>();
         clientInteractionComponentWriter.SendInteractEvent(new InteractionRequest(linkedEntityComponent.EntityId));
     }
 
     public class NearbyInteractable
     {
         public ClientInteractable interactable;
         public ItemInteractionButton itemInteractionButton;
 
         public NearbyInteractable(ClientInteractable clientInteractable, ItemInteractionButton button)
         {
             interactable = clientInteractable;
             this.itemInteractionButton = button;
         }
     }

Where uiManager just has this subclass:

 [System.Serializable]
     public class InteractionDisplay
     {
         public GameObject itemPanel, interactionPanel;
 
         public GameObject itemInteractionButtonPrefab;
         public Transform itemPanelContent;
 
         public ItemInteractionButton AddItemButton(ClientInteractable clientInteractable)
         {
             ItemInteractionButton itemInteractionButton = Instantiate(itemInteractionButtonPrefab, itemPanelContent).GetComponent<ItemInteractionButton>();
             itemInteractionButton.itemIcon.sprite = clientInteractable.interactionIcon;
             itemInteractionButton.itemText.text = clientInteractable.interactionMessage;
             return itemInteractionButton;
         }
 
         public void ShowItemDisplay(bool show)
         {
             if (itemPanel.activeSelf != show)
             {
                 itemPanel.SetActive(show);
             }
         }
 
         public void ShowDisplay(bool show)
         {
             if (interactionPanel.activeSelf != show)
             {
                 interactionPanel.SetActive(show);
             }
         }
     }





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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by NotEth4n · May 22, 2019 at 04:09 AM

After some more messing around, it turns out that the UI Buttons were being created and destroyed essentially at the same time, which would create and clear them in the Scene View and Game View fast enough to be seen as still being there, but not be clicked! This is due to the difference between the distances using Collider2D[] nearbyColliders = Physics2D.OverlapCircleAll(transform.position, interactionRange, interactableLayers); and Vector2.Distance(nearbyInteractables[i].interactable.transform.position,transform.position)

The issue was that Physics2D.OverlapCircleAll() detects Colliders even if just the edge is within range, while Vector2.Distance() returns the distance to the transform's origin (typically the center)! This of course creates the issue where the OverlapCircle is touching the edge, but not the object's origin.

I have not thought of a good way to get the correct distance to the closest edge yet other than to use Collider.Bounds.ClosestPoint() which I am sure would not be very good for performance many times per frame...

Comment

People who like this

0 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

222 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

one button to hide and unihed gameobjects 0 Answers

Destroy a Gameobject with a UI Button 3 Answers

Unity ads , can't we show video by clicking a button? 1 Answer

Issue with spawning buttons and assigning listeners 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