- Home /
Wierd Console Errors
Alright so here is my code and I am getting errors. I need help guys. Below is my code.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static int score = 0;
public Text scoreText;
public Text bestText;
void Start ()
{
DontDestroyOnLoad (gameObject);
DontDestroyOnLoad (scoreText);
DontDestroyOnLoad (bestText);
DontDestroyOnLoad (score);
if (score > PlayerPrefs.GetInt (Application.loadedLevel + "Best"))
{
PlayerPrefs.SetInt (Application.loadedLevel + "Best", score);
//Play new high score animation
scoreText.text = "" + score;
bestText.text = "" + PlayerPrefs.GetInt (Application.loadedLevel + "Best");
}
}
void Update ()
{
scoreText.text = "" + score;
bestText.text = "" + PlayerPrefs.GetInt (Application.loadedLevel + "Best");
}
}
Here are the errors:
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:93) UnityEngine.UI.Text.set_text (System.String value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Text.cs:140) ScoreManager.Update () (at Assets/Scripts/ScoreManager.cs:29)
Answer by Bunny83 · Jun 30, 2015 at 06:12 AM
DontDestroyOnLoad only works on root gameobjects or on components that are located on a root object. If you use it on a child object but not on the parent it have no effect at all.
That means if you load a new level the object is destroyed anyways.
If you use DontDestroyOnLoad on the root object that object and all of it's child objects won't be destroyed when you load a new level.
Answer by superbsumit · Jun 29, 2015 at 06:27 AM
You have not referenced your 'scoreText' and 'bestText' in you Inspector panel. Just drag n drop the respective Text file in the Inspector.
@superbsumit thanks for your reply but it is so wierd because the error occurs even when i have already done that.
Where is this script attached? I think you are continuously fetching 'scoreText.text' even after you load your next scene.
The next scene will not contain the reference of your objects which are set through Inspector, even the gameObject is set to DontDestroyOnLoad.
Also, this part of your code is not valid:
DontDestroyOnLoad (scoreText);
DontDestroyOnLoad (bestText);
DontDestroyOnLoad (score);
Please provide some more info on how are you using this script.
@superbsumit Here is a basic rundown :
I have two scenes that utilize this scoretext and best text. The game scene and game over scene. Atm best is rendered invisible in game scene. I put dont destroy on load so I can used the same values again to render the best and score in game over. The script is attached to a game object called scoremanager in both my game scene and game over.
Thanks for the help btw
Ok, as per my understanding, scoremanager gameObject is set to DontDestroyOnLoad and you are getting the score in the next scene from this script only!!!
$$anonymous$$y Confusion: Is the 'scoremanager' gameObject is already present in both the scenes from start??
One simple way you can get your current score to next scene is making 'score' static and simply accessing it in other script, which you are already doing (but I don't see, that where are you setting/increasing your 'score'? It will always be zero). And you can also access the 'highScore' by using PlayerPrefs (same way you are doing). You don't really have to use DontDestroyOnLoad method. And the 'scoremanager' gameObject should only be present in the game scene.
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 0 Answers
Compiler Warnings Are Now Errors 0 Answers
How to disable specific errors in the console 1 Answer
Calling 'AddItem' Method giving me error NullReferenceException: 0 Answers
Why am I getting NullReferenceException on my .Contains()? 1 Answer