Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug [2020.2.0b10, case 1290917] Destroyed UI object can be added to raycasts breaking whole UI

Discussion in '2020.2 Beta' started by Kamyker, Nov 7, 2020.

  1. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,085
    New raycastTarget is defined as:

    Code (CSharp):
    1.  
    2. public virtual bool raycastTarget
    3. {
    4.     get
    5.     {
    6.         return m_RaycastTarget;
    7.     }
    8.     set
    9.     {
    10.         if (value != m_RaycastTarget)
    11.         {
    12.             if (m_RaycastTarget)
    13.                 GraphicRegistry.UnregisterRaycastGraphicForCanvas(canvas, this);
    14.             m_RaycastTarget = value;
    15.             if (m_RaycastTarget)
    16.                 GraphicRegistry.RegisterRaycastGraphicForCanvas(canvas, this);
    17.         }
    18.     }
    19. }
    20.  
    There's no null check there or in RegisterRaycastGraphicForCanvas so it can be used freely on destroyed object:

    Code (CSharp):
    1. button.onClick.AddListener( async () =>
    2.     {
    3.         var graphic = button.targetGraphic;
    4.         graphic.raycastTarget = false;
    5.         Destroy( button.gameObject );
    6.         graphic.raycastTarget = true;
    7.     } );
    Doing that breaks whole ui input.




    Bug doesn't exist in 2020.1
     
    Last edited: Nov 7, 2020
  2. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,085
    A bit better code to reproduce as object is destroyed after update loop:

    Code (CSharp):
    1. button.onClick.AddListener( async () =>
    2.     {
    3.         var graphic = button.targetGraphic;
    4.         graphic.raycastTarget = false;
    5.         Destroy( button.gameObject );
    6.         await Task.Delay( 100 );
    7.         graphic.raycastTarget = true;
    8.     } );
     
  3. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,132
    Hi @Kamyker,

    Thanks for the report. We'll look into it.
     
  4. MafiaMoe

    MafiaMoe

    Joined:
    Mar 24, 2013
    Posts:
    27
    I can confirm this happens in our project as well using 2020.2.0f1, although we found an easy work around.

    For Unity Devs: Perhaps check if the graphic object is alive when the user sets graphic.raycastTarget to true? Could that do the trick?
     
  5. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,132
    JamesArndt likes this.