Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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,084
    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,084
    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,111
    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,111
    JamesArndt likes this.