Following along with all the Laser Defender lessons thus far, my Laser Defender works and incidentally (looks and sounds cool) except, after adding a Health display on my Game level, my GameOver level score started displaying a 0 instead of displaying the current score as it had prior to my adding a Health score display. I’ve done lots of debugging and don’t know what to share here to possibly get community help to figure out what the bug is I’ve created. Any suggestions would be appreciated.
Hey Jim
The first thing I would suggest trying is popping a Debug.Log
statement in the code you have which is responsible for displaying the score on the GameOver scene. If you use that to output the score to the console first, you can then see whether the UI and your code are reporting things correctly, but perhaps you’ve lost a reference to whatever is maintaining the score, or, whether something else is at fault.
Give this a go and see what happens. If you want some additional help, share your project files with me here and I will happily take a look for you.
The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.
I kept fiddling with things and debugging then started getting this error message:
So I’ll keep working at this.
MissingReferenceException: The object of type ‘GameSession’ 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.
GameSession.GetScore () (at Assets/Scripts/GameSession.cs:40)
ScoreDisplay.Update () (at Assets/Scripts/ScoreDisplay.cs:26)
Now, sometimes the score displays on GameOver and sometimes it doesn’t and I’m now getting the following warning:
DontDestroyOnLoad only work for root GameObjects or components on root GameObjects.
UnityEngine.Object:DontDestroyOnLoad(Object)
ScoreDisplay:SetUpSingleton() (at Assets/Scripts/ScoreDisplay.cs:25)
ScoreDisplay:Awake() (at Assets/Scripts/ScoreDisplay.cs:15)
And now it works, just had to delete the GameSessions GameObject in the GameOver Scene.
Glad I could be of help Jim! LOL… I was asleep during that period of time, sorry.
Really glad you have resolved the issue yourself though and well done for plugging away at it
I had the same issue when loading from the Game scene to the Game Over scene. Using the Debug.Log I found the code to be working correctly and instead discovered that the script was trying to load the GameSession for the current scene. The fix for this was to use gameObject.SetActive(false) before destroying the gameObject in the SetUpSingleton method. Hope this helps!
Thanks @Ben_R! I was hunting for ages and totally forgot the issue from before.
For anyone else having the same bug, your singleton code needs to have gameObject.SetActive(false) before the destroy line like this:
private void Awake()
{
SetUpSingleton();
}
private void SetUpSingleton()
{
if (FindObjectsOfType(GetType()).Length > 1)
{
gameObject.SetActive(false);
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}