Missing Reference Exception
Hi guys. I am trying to make a game object, in this case a battery, spawn around the room. i have created a cylinder with a battery texture and put it in the scene.
using UnityEngine;
using System.Collections;
public class Battery_Script : MonoBehaviour {
public Transform[] spawns;
void Start () {
InvokeRepeating("Spawn", 5 , 5);
}
void Spawn() {
int i = Random.Range (0,spawns.Length);
transform.position = spawns[i].position;
transform.rotation = spawns[i].rotation;
}
}
i have attached this script to it and i keep getting the error message
"MissingReferenceException: The object of type 'Battery_Script' 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. UnityEditor.GenericInspector.GetOptimizedGUIBlock (Boolean isDirty, Boolean isVisible, UnityEditor.OptimizedGUIBlock& block, System.Single& height) (at C:/buildslave/unity/build/Editor/Mono/Inspector/GenericInspector.cs:19) UnityEditor.InspectorWindow.FlushOptimizedGUIBlock (UnityEditor.Editor editor) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1430)"
i think its only being called once because collapse isn't checked on the console so i assume its happening on start.
i don't destroy it at any point so i'm stumped.
i'm sure i'm missing something obvious :) so thanks in advance.
Answer by hexagonius · Sep 13, 2015 at 03:38 PM
It's not this code that generates the error. Something else is using the battery, tries to access it and fails. Maybe you referenced the battery in the inspector at some point and changed the script name later or something. In any case, wherever you reference a GameObject with this script attached to, reassign it.
just open and closed unity and i'm not getting the error anymore. $$anonymous$$ay be a bug in 5.2. Either way thanks for the answer.
Thanks for the tip! I had the same error just now. Restarted Unity and its gone. Seems like a bug to me too.