In my game it’s game over if an enemy touches the player. When this happens they are taken to the game over screen which also shows their score. Currently whenever the player gets a game over the score on the game over screen is shown as 0. I know the score script is working properly because it shows the correct score in the inspector when I click on the score object. So it’s probably either the game manager or game over screen script.
Score script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Playerscore : MonoBehaviour
{
public static float scoreValue = 0f;
public float ChangePerSecond;
Text score;
// Start is called before the first frame update
void Start()
{
score = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
scoreValue += ChangePerSecond * Time.deltaTime;
score.text = ""+ (int)scoreValue;
}
}
Game over screen script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public void Setup(int score)
{
gameObject.SetActive(true);
pointsText.text = score.ToString() + " Points";
}
}
Game manager script
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public int scoreValue;
public GameOverScreen GameOverScreen;
bool gameHasEnded = false;
public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
//Debug.Log("game over");
GameOverScreen.Setup(scoreValue);
}
}
}