Author Topic: [SOLVED]Destroying Gameobject generated from Prefab with UILabel exception  (Read 2002 times)

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
As the title says i am using NGUITools.Destroy(); to destroy a game object and unity standard one, either way i get the following exception...

Quote
MissingReferenceException: The object of type 'UILabel' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UIPanel.UpdateWidgets () (at Assets/Imported/NGUI/Scripts/UI/UIPanel.cs:1519)
UIPanel.UpdateSelf () (at Assets/Imported/NGUI/Scripts/UI/UIPanel.cs:1222)
UIPanel.LateUpdate () (at Assets/Imported/NGUI/Scripts/UI/UIPanel.cs:1181)

This never happened before i started using my own font's, dynamic i believe. I also did some research into it and people seem to suggest you do the following in UIWidget

  1. public virtual void MarkAsChanged ()
  2.         {
  3.                 if (NGUITools.GetActive(this))
  4.                 {
  5.                         mChanged = true;
  6. #if UNITY_EDITOR
  7.             if(this != null)<--ADD THIS CHECK HERE
  8.             {
  9.                             NGUITools.SetDirty(this);
  10.             }
  11. #endif
  12.                         // If we're in the editor, update the panel right away so its geometry gets updated.
  13.                         if (panel != null && enabled && NGUITools.GetActive(gameObject) && !mPlayMode)
  14.                         {
  15.                                 SetDirty();
  16.                                 CheckLayer();
  17. #if UNITY_EDITOR
  18.                                 // Mark the panel as dirty so it gets updated
  19.                                 if (material != null) NGUITools.SetDirty(panel.gameObject);
  20. #endif
  21.                         }
  22.                 }
  23.         }
  24.  

but to no avail. Does anyone have any idea how to fix this?

UPDATE:

So i discovered...If you spawn a prefab with a UILabel that starts of as inactive...destroying it will cause the crash, they needed to be active at some point during the lifetime of the object before you destroy it, so it is initialisations being ignored based on an inactive state. I don't know if this only applies to UILabels or other NGUI elements.

On a slightly related note i had to change certain code in NGUI to take into account inactive elements also on the NGUITools.BringForward() as it would ignore inactive elements in and under the object you are trying to bring forward and would mess up the visual hierarchy of the object. Whether or not this was intended i do not know, but if you are experiencing a similar malady, with NGUI it seems better to have all elements active until the components Start() get's called at which point you can set it too invisible or inactive.

Good luck.
« Last Edit: November 21, 2014, 03:20:39 AM by Genhain »