How to fetch and add total score to a Win scene?

I almost finished the Block Breaker, while I noticed it would be better add a Win scene in the end,
and show “Your score is: xxxx” on the center of the screen.

I’m thinking which way is the best way to make it, as I noticed we can’t copy Game Session prefab, because the score text is designed for the levels, on the right top corner.

I suppose I should define a public variable for Total Score in Gamesession object, then create a “Win” script in the Win scene, to fetch the variable from Gamesession object and show in a text box?

but I have some issue when I tried to “fetch the variable from Gamesession object” in the Win script.

May I have some suggestion about it?

Check that out:

https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Thanks Luke, I have managed to get the total score in the new script “win”, and showed it to the center of screen.
I created another method TotalScore() in the “GameSession” script to return “current score” in the “GameSession” script, and call that in the “win” script.

public class Win : MonoBehaviour
{
// config params
[SerializeField] TextMeshProUGUI scoreText;

GameSession theGameSession;

// Start is called before the first frame update
void Start()
{
    theGameSession = FindObjectOfType<GameSession>();
    scoreText.text = theGameSession.TotalScore().ToString();
}

}

But, the new problem is, as you said, the “GameSession” is lunched as Object.DontDestroyOnLoad, so the score on the right top corner always showing. How could I dismiss that in the “Win” scene while don’t affect get totalScore from GameSession?

Could you please post a screenshot of the game scene hierarchy and the object the GameSession script is attached to.

Here is the screenshot when I tried to show the score in the Win scene, which is returned from the GameSession , but original score still showing.

So, I think my main confusion is that how to destroy the DontDestryoOnLoad
object GameSession in the “win” scene.

and here is my scripts of both GameSession.cs and Win.cs:Scripts.zip (1.2 KB)

hierarchy of game scene and win scene:


Try that:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameSession : MonoBehaviour {

    int score = 0;

    private void Awake()
    {
        SetUpSingleton();
    }

    private void SetUpSingleton()
    {
        int numberGameSessions = FindObjectsOfType<GameSession>().Length;
        if (numberGameSessions > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }

    public int GetScore()
    {
        return score;
    }

    public void AddToScore(int scoreValue)
    {
        score += scoreValue;
    }

    public void ResetGame()
    {
        Destroy(gameObject);
    }

 }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreDisplay : MonoBehaviour {

    Text scoreText;
    GameSession gameSession;

	// Use this for initialization
	void Start () {
        scoreText = GetComponent<Text>();
        gameSession = FindObjectOfType<GameSession>();
	}
	
	// Update is called once per frame
	void Update () {
        scoreText.text = gameSession.GetScore().ToString();
	}
}

Attach the GameSession script to an empty game object and the ScoreDisplay script to the text objects.

Thanks for your patient reply.

I finally figured out it,
though I had done this as you showed, my script was “Win.cs” instead of “ScoreDisplay” as I attached.

The only different is I need to separate the score display command from GameSession of Rick’s sample.

Thank you !
But I’m still curious about how to end a DontDestryoOnLoad game object, I didn’t find the method from documentory.

Check that out:

https://answers.unity.com/questions/1242335/how-to-end-the-function-dontstoponload-when-changi.html

There is no undo for that function.

Ok, got it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms