Author Topic: MissingReferenceException: The object of type 'UIGamePanel' has been destr  (Read 2475 times)

Der_Kevin

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
Hi!
I am having the problem that, as soon as I am switching a Scene my NGUI Ui does not work anymore. The Resource Bars and Buttons that show from the beginning are working perfectly. But the Panels are not working anymore. i got the following error message:

MissingReferenceException: The object of type 'UIGamePanel' 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. UnityEngine.Component.get_gameObject () UIGamePanel.Hide () UIGamePanel.cs:73) UIGamePanel.HideActivePanels () UIGamePanel.cs:153)

my UIGamePanel.cs looks like this:

  1. #define USE_NGUI_2X
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6.  
  7. public class UIGamePanel : MonoBehaviour {
  8.        
  9.         public const float UI_DELAY = 0.75f;
  10.         public const float UI_TRAVEL_DIST = 1.6f;
  11.        
  12.         public GameObject content;
  13.         public PanelType panelType;
  14.         public PanelType openFromPanelOnly;
  15.  
  16.         /**
  17.          * Position of the buttons when visible.
  18.          */
  19.         protected Vector3 showPosition;
  20.        
  21.         /**
  22.          * Position of the buttons when hidden.
  23.          */
  24.         protected Vector3 hidePosition;
  25.        
  26.         public static Dictionary <PanelType, UIGamePanel> panels;
  27.  
  28.         public PanelType[] IgnoreTypes;
  29.  
  30.         void Awake() {
  31.                 if (panels == null) panels = new Dictionary <PanelType, UIGamePanel> ();
  32.                 if (activePanels == null) activePanels = new List<UIGamePanel> (0);
  33.  
  34.                 panels.Add (panelType, this);
  35.                 showPosition = content.transform.position;
  36.                 hidePosition = content.transform.position - new Vector3(0, UI_TRAVEL_DIST, 0);
  37.                 if (panelType == PanelType.DEFAULT) {
  38.                         activePanels.Add(this);
  39.                 } else {
  40.                         content.transform.position = hidePosition;     
  41.                 }
  42.                
  43.                 Init ();
  44.         }
  45.  
  46.         virtual protected void Init() {
  47.         }
  48.        
  49.         virtual public void InitialiseWithBuilding(Building building) {
  50.         }
  51.        
  52.         virtual public void Show() {
  53.  
  54.                 if (panelType == PanelType.DEFAULT && GridView.Instance != null) GridView.Instance.NormalMode();
  55.                 if (panelType == PanelType.PLACE_BUILDING && GridView.Instance != null) GridView.Instance.BuildingMode();
  56.                 if (panelType == PanelType.EDIT_PATHS && GridView.Instance != null) GridView.Instance.PathMode();
  57.                 if (activePanels.Contains(this)) {
  58.                         StartCoroutine(DoReShow());
  59.                 } else if (activePanels.Count == 0 || IsPanelOpenedFromActivePanel () || openFromPanelOnly == PanelType.NONE) {
  60.                         HideActivePanels ();
  61.  
  62.                         gameObject.SendMessage ("OnShow", this, SendMessageOptions.DontRequireReceiver);
  63.                         StartCoroutine(DoShow());
  64.                         activePanels.Add (this);
  65.                 }
  66.         }
  67.  
  68.         virtual public void Hide() {
  69.  
  70.                 if (activePanels.Contains(this)) {
  71.                         activePanels.Remove (this);
  72.                 }
  73.                 gameObject.SendMessage ("OnHide", this, SendMessageOptions.DontRequireReceiver);
  74.                 StartCoroutine(DoHide());
  75.         }
  76.  
  77.         public static void ShowPanel(PanelType panelType) {
  78.                 if (panelType == PanelType.DEFAULT) BuildingManager.ActiveBuilding = null;
  79.                 if (panels.ContainsKey (panelType)) {
  80.                         //if (!activePanels.Contains(panels[panelType])) {
  81.                                 panels [panelType].Show ();
  82.                         //}
  83.                 }
  84.         }
  85.  
  86.         public static List<UIGamePanel> activePanels;
  87.                
  88.         /**
  89.          * Reshow the panel (i.e. same panel but for a different object/building).
  90.          */
  91.         virtual protected IEnumerator DoReShow() {
  92.                 gameObject.BroadcastMessage ("OnHide", SendMessageOptions.DontRequireReceiver);
  93.                 iTween.MoveTo(content, hidePosition, UI_DELAY);
  94.                 yield return new WaitForSeconds(UI_DELAY / 3.0f);
  95.  
  96.                 gameObject.BroadcastMessage ("OnShow", SendMessageOptions.DontRequireReceiver);
  97.                 iTween.MoveTo(content, showPosition, UI_DELAY);
  98.         }
  99.        
  100.        
  101.         /**
  102.          * Show the panel.
  103.          */
  104.         virtual protected IEnumerator DoShow() {
  105.                 yield return new WaitForSeconds(UI_DELAY / 3.0f);
  106.                 content.SetActive (true);
  107.  
  108.                 gameObject.BroadcastMessage ("OnShow", SendMessageOptions.DontRequireReceiver);
  109. #if USE_NGUI_3X
  110.                 yield return true;
  111.                 GetComponent<UIPanel>().Refresh();
  112. #endif
  113.                 iTween.MoveTo(content, showPosition, UI_DELAY);
  114.         }
  115.        
  116.         /**
  117.          * Hide the panel.
  118.          */
  119.         virtual protected IEnumerator DoHide() {       
  120.                 gameObject.BroadcastMessage ("OnHide", SendMessageOptions.DontRequireReceiver);
  121.  
  122.                 iTween.MoveTo(content, hidePosition, UI_DELAY);
  123.                 yield return new WaitForSeconds(UI_DELAY / 3.0f);
  124.                 content.SetActive (false);
  125.         }
  126.  
  127.         protected bool IsPanelOpenedFromActivePanel () {
  128.                 for (int i = 0; i < activePanels.Count; ++i) {
  129.                         if (activePanels[i].panelType == openFromPanelOnly) {
  130.                                 return true;
  131.                         }
  132.                 }
  133.  
  134.                 return false;
  135.         }
  136.  
  137.         protected void HideActivePanels () {
  138.                 if (activePanels != null) {
  139.                         for (int i = 0; i < activePanels.Count; ++i) {
  140.                                 UIGamePanel panel = activePanels[i];
  141.                                 bool hide = true;
  142.                                 if (IgnoreTypes != null) {
  143.                                         for (int j = 0; j < IgnoreTypes.Length; ++j) {
  144.                                                 if (IgnoreTypes[j] == panel.panelType) {
  145.                                                         hide = false;
  146.                                                         break;
  147.                                                 }
  148.                                         }
  149.                                 }
  150.                                
  151.                                 if (hide) {
  152.                                         activePanels.Remove (panel);
  153.                                         panel.Hide ();
  154.                                         --i;
  155.                                 }
  156.                         }
  157.                 }
  158.         }
  159. }

does anyone of you having this problem before?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
You have a static list. It persists as you switch scenes. If you add stuff to it, you also need to make sure to remove it prior to switching the scene. This isn't an NGUI question, but a C#/Unity question.