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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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
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

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

GameObject.Destroy(gameObject) does not destroy capsules 2 Answers

Destory button on click and other problem 0 Answers

On Button Click Enable - Disable GameObject Through Inspector 1 Answer

NullReferenceExeption on gameObject.SetActive 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