Search Unity

MissingReferenceException

Discussion in 'UGUI & TextMesh Pro' started by vrm2p, Oct 2, 2014.

  1. vrm2p

    vrm2p

    Joined:
    Jul 23, 2014
    Posts:
    17
    Hi,

    I experienced a strange behaviour after updating to b20. After upgrading a dynamically populated GridLayoutgroup inside of a ScrollRect is causing a MissingReferenceException, which I am unable to fix:

    The error is caused by the EventSystem. If I remove the EventSystem out of the Hierarchy all is running fine (except that no events fire).
    I'd file this as a bug, but my project is already 1.1GB and I cannot reproduce this error in a newly created project :-(
    Perhaps someone else got an idea what is causing this to happen.

    (at the time the error occurs all images are loaded and displayed without any problems - it's just the event system completely messing up. As you can see in the stacktrace, no custom script is involved)

    Code (csharp):
    1. MissingReferenceException: The object of type 'Image' has been destroyed but you are still trying to access it.
    2. Your script should either check if it is null or you should not destroy the object.
    3. UnityEngine.Component.GetComponent[CanvasRenderer] () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineComponent.cs:189)
    4. UnityEngine.UI.Graphic.get_canvasRenderer () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Graphic.cs:193)
    5. UnityEngine.UI.Graphic.get_depth () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Graphic.cs:154)
    6. UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.Canvas canvas, UnityEngine.Camera eventCamera, Vector2 pointerPosition, System.Collections.Generic.List`1 results) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/GraphicRaycaster.cs:180)
    7. UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List`1 resultAppendList) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/GraphicRaycaster.cs:104)
    8. UnityEngine.EventSystems.EventSystem.RaycastAll (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List`1 raycastResults) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/EventSystem.cs:134)
    9. UnityEngine.EventSystems.PointerInputModule.GetMousePointerEventData () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/InputModules/PointerInputModule.cs:145)
    10. UnityEngine.EventSystems.StandaloneInputModule.ProcessMouseEvent () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/InputModules/StandaloneInputModule.cs:276)
    11. UnityEngine.EventSystems.StandaloneInputModule.Process () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/InputModules/StandaloneInputModule.cs:159)
    12. UnityEngine.EventSystems.EventSystem.Update () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/EventSystem.cs:228)
    13.  
    EDIT: the exceptions are only fired while the game window is active. (App.runInBackground is true) There's no error or exception in the population process of the list


    any help is greatly appreciated :)
     
    Last edited: Oct 2, 2014
  2. kennyd-itp

    kennyd-itp

    Joined:
    Jan 13, 2014
    Posts:
    30
    I am having the same issue.

    I have a hierachy of views (canvas renderers with child controls) that I'm going through, which works fine - when switching views I instantiate the new one and then destroy the old one. But after transitioning between one of these views to the next I keep getting this exception as well. This seems fairly random; going from view 1, 2, 3 works great, then I hit view number 4 and this happens.

    It's as if the event system is trying to keep raycasting against controls that no longer exist - but then why doesn't it throw these exceptions every time I Destroy a UI object?
     
  3. vrm2p

    vrm2p

    Joined:
    Jul 23, 2014
    Posts:
    17
    this is a good hint into the right direction. My List contains 2 entries in the editor view, which I use for editing the prefabs.
    On Runtime, these entries are removed and the list is repolulated. If I comment out the code which empties the list the exception does not occur. Maybe it's the wrong way to clear the list

    Code (CSharp):
    1. RectTransform[] existingElements = panel.GetComponentsInChildren<RectTransform> ();
    2. for (int j =0; j< existingElements.Length; j++) {
    3.     if (existingElements [j].gameObject.name == panel.name) {
    4.         continue;
    5.     }
    6.      
    7.     if (existingElements [j].gameObject != null) {
    8.         GameObject.Destroy (existingElements [j].gameObject);
    9.     }
    10. }
     
  4. kennyd-itp

    kennyd-itp

    Joined:
    Jan 13, 2014
    Posts:
    30
    OK, I seem to have solved my instance of the issue in an unexpected way.

    One of the controls on the view that was causing the exception after being deleted, was a button that I set inactive by default:

    Code (CSharp):
    1. void Awake()
    2. {
    3.     SwitchCameraButton.gameObject.SetActive(false);
    4. }
    However, if I disable the button in Start() instead of Awake(), I don't get any exceptions when I Destroy it. It seems the Image component or its object needs to be active at Awake-time in order for the event system to handle it properly.

    This workaround seems to fix my case for now:

    Code (CSharp):
    1. void Awake()
    2. {
    3.     SwitchCameraButton.gameObject.SetActive(true);
    4. }
    5.  
    6. void Start()
    7.  {
    8.     SwitchCameraButton.gameObject.SetActive(false);
    9.  }
    @Unity GUI team: is this normal behavior, or a bug? Or is there a better way altogether to completely hide UI controls so I can avoid using SetActive() ?

    @vrm2p: does this apply to your case as well? Can't say for sure, it would depend on how and when you populate your ScrollRect I guess.
     
  5. vrm2p

    vrm2p

    Joined:
    Jul 23, 2014
    Posts:
    17
    This seem to be 2 completely different bugs leading to the same result. I don't have any disabled controls (non-interactable, but not disabled)

    EventSystem.RaycastAll seems to find already deleted objects, that are not completely removed.

    Edit: still cannot reproduce in a demoproject :-(
     
    Last edited: Oct 2, 2014
  6. AlienMe

    AlienMe

    Joined:
    Sep 16, 2014
    Posts:
    93
    Had the same issue after updating to b20. I have a ScrollView with a Grid Layout Group as the content. Whenever I empty the Grid Layout Group, I get the same exception as the op.

    Managed to work around this issue by calling GraphicRegistry.UnregisterGraphicForCanvas on each Image component before destroying the Grid Layout Group's children.
     
  7. vrm2p

    vrm2p

    Joined:
    Jul 23, 2014
    Posts:
    17
    This fixed it for me. Many thanks!

    @Tim C is this a bug or working as intended?
     
  8. Andy-Block

    Andy-Block

    Joined:
    Aug 10, 2012
    Posts:
    10
    Also had the same issue on b20. @kennyd-itp : you're solution worked for me too - thanks!
     
  9. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    I would love to fix this in the framework if I can grab a repro case.
     
  10. davebuchhofer

    davebuchhofer

    Joined:
    Nov 9, 2007
    Posts:
    126
    I've got a project that repro's it, though the steps to get to the repro are a bit arcane, I'll send it through and get back with the number once it arrives :)
     
  11. davebuchhofer

    davebuchhofer

    Joined:
    Nov 9, 2007
    Posts:
    126
    Here you go Tim! (Case 639738)
     
  12. TheDelhiDuck

    TheDelhiDuck

    Joined:
    Aug 29, 2014
    Posts:
    35
    Thanks for sharing this one, saved me a lot of time!
     
  13. PW2006

    PW2006

    Joined:
    Sep 16, 2014
    Posts:
    1
    Hey I fixed this problem by making sure gameObject.SetActive(false); before Destroying it. I think setting it to inactive effectively removes it from the EventSystem.
     
    billgeekza likes this.