- Home /
Fixed ^^
I have an error "MissingReferenceException" and i dont know why
So..i creating an score and i works, when i kill the enemy it adds +10 to the score,when i get hit it quits 5 points to it, so everything works properly but i have this error all the time,i know my score works properly always but i dont understand why it appears. (Im new,maybe its just a silly error but i really dont know)
Error:
MissingReferenceException: The object of type 'Text' 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.EventSystems.UIBehaviour.IsActive () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/UIBehaviour.cs:22) UnityEngine.UI.Graphic.SetVerticesDirty () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Graphic.cs:100) UnityEngine.UI.Text.set_text (System.String value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Text.cs:138) PlayerController.SetScore () (at Assets/PlayerController.cs:133) PlayerController.OnBecameInvisible () (at Assets/PlayerController.cs:99)
Answer by Akusan · Sep 06, 2017 at 07:53 PM
You need to add a check on your script if enemy exists or not before accessing it's text variable. What happens is that you destroyed the enemy game object but you have some functions trying to access that destroyed game object's variables.
Check if null, and if it is null, either add a return statement or create a target for your script. Here's an example using if null:
private static Level$$anonymous$$anager instance = null;
public static Level$$anonymous$$anager GetInstance(){
if (instance == null) {
instance = new Level$$anonymous$$anager ();
instance.coloredList = new List<Color> ();
instance.coloredList.Add (Color.red);
instance.coloredList.Add (Color.yellow);
instance.coloredList.Add (Color.blue);
}
Follow this Question
Related Questions
Weird Error after Update 4 Answers
Errors i can't understand. SetSystemInterested 1 Answer
Monodevelop Fatal Error 0 Answers
Unity keeps saying that the "Sequence contains no elements" 2 Answers