Weird MissingReferenceException
Iam getting a weird error that i cannot figure out;
The error MissingReferenceException: The object of type 'RUI_ArrayButton' 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. RUI_ArrayButton.ChangeImage (UnityEngine.Texture2D _newimage) (at Assets/Resources/RUI/Scripts/RUI_ArrayButton.cs:17) RUI_TabController.RefreshSprite () (at Assets/Resources/RUI/Scripts/RUI_TabController.cs:88) RUI_TabController.RefreshTabamount () (at Assets/Resources/RUI/Scripts/RUI_TabController.cs:137) RUI_TabEditor.TabamountEditor (.RUI_TabController _tabcontroller) (at Assets/Scripts/Editor/RUI/RUI_TabEditor.cs:65) RUI_TabEditor.OnInspectorGUI () (at Assets/Scripts/Editor/RUI/RUI_TabEditor.cs:20)
When does this happen? When i decrease the tabamount by any amount it throws this error. Other than this error everything seems to be working fine but i dont think unity throws this error for nothing.
The code thats causing this error:
public void ChangeImage(Texture2D _newimage){
if (ButtonImage == null) {
Debug.Log (gameObject);
ButtonImage = gameObject.GetComponent<RawImage> ();
}
ButtonImage.texture = _newimage;
gameObject.GetComponent<RectTransform> ().sizeDelta = new Vector2 (_newimage.width, _newimage.height);
}
Some more code (RUI_TabController class)
public void RefreshTabamount(){
if (Tabobjects.Length != Tabamount) {
RefreshCheck ();
if (Tabobjects.Length > Tabamount) {
int AmountToDestroy = Tabobjects.Length - Tabamount;
for (int i = 0; i < AmountToDestroy; i++) {
DestroyImmediate (Tabobjects [i]);
}
Tabobjects = gameObject.GetAllChildren ();
}
if (Tabobjects.Length < Tabamount) {
int AmountToInstantiate = Tabamount - Tabobjects.Length;
for (int i = 0; i < AmountToInstantiate; i++) {
GameObject tab = new GameObject ();
GameObject text = new GameObject ();
tab.transform.SetParent (gameObject.transform);
text.transform.SetParent (tab.transform);
tab.name = "Tab" + i;
text.name = "Text";
tab.AddComponent<RUI_ArrayButton> ();
Text tabtext = text.AddComponent<Text> ();
tabtext.raycastTarget = false;
RectTransform tabrect = tab.GetComponent<RectTransform> ();
tabrect.localScale = new Vector3 (1, 1, 1);
tabrect.anchorMax = new Vector2 (0, 0);
tabrect.anchorMin = new Vector2 (0, 0);
RectTransform textrect = text.GetComponent<RectTransform> ();
textrect.anchoredPosition = new Vector2 (0.0f, -0.75f);
}
Tabobjects = null;
Tabobjects = gameObject.GetAllChildren ();
RA = null;
RA = new RUI_ArrayButton[Tabobjects.Length];
for (int i = 0; i < Tabobjects.Length; i++) {
RA [i] = Tabobjects [i].GetComponent<RUI_ArrayButton> ();
}
}
RefreshSpacing ();
RefreshText ();
RefreshSprite ();
}
}
More code (the custom editor function that is called from the OnInspectorGUI method)
private void TabamountEditor(RUI_TabController _tabcontroller){
GUILayout.BeginHorizontal ();
EditorGUILayout.LabelField ("Tabs",GUILayout.Width (60));
EditorGUI.BeginChangeCheck();
_tabcontroller.Tabamount = EditorGUILayout.IntSlider (_tabcontroller.Tabamount,1,100, GUILayout.Width (200));
GUILayout.EndHorizontal ();
if (EditorGUI.EndChangeCheck ()) {
_tabcontroller.RefreshTabamount ();
}
}
Your answer
Follow this Question
Related Questions
Destroying GameObject while in Inspector = MissingReferenceException 0 Answers
Unity error The object of type 'Preview' has been destroyed but you are still trying to access it. 0 Answers
Help Simple adding a GameObject to an ObjectField() 1 Answer
The referenced script on this Behaviour (Game Object 'FirstPersonCharacter') is missing! 1 Answer