I want to display the score of the last game in the start menu scene, so that when a player finishes the game and is returned to the start menu scene, their score is displayed there…
I have an empty game object in the start menu that the ‘ScoreManager’ script is attached to, which I’ve put to not destroy on load - this is linked to a 3D text. I also have two scripts that deduct and add points, calling the function in the ScoreManager script to update the total score. Can anyone tell me where I’ve gone wrong or how to make this work please? The code is as follows:
public class ScoreManager : MonoBehaviour {
public static int score;
public TextMesh Scoretext;
//Add points to score and trigger UpdateScore
public void AddPoints(int points)
{
score += points;
UpdateScore();
}
//Update score text
public void UpdateScore()
{
Scoretext.text = "Latest
Score:
" + score;
}
}
public class SmCollider : MonoBehaviour {
Vector3 Origin;
public AudioClip SmHit;
public Spawner SpawnPos;
public int addScore = 10;
public ScoreManager scoremanager;
// Detect collision w/snowman, play audio, destroy snowman and add points
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Snowman")
{
Debug.Log("Hit snowman");
Origin = SpawnPos.spawnPos;
AudioSource.PlayClipAtPoint(SmHit, Origin);
Destroy(gameObject);
Destroy(collision.gameObject);
scoremanager.AddPoints(addScore);
}
}
}
public class SbCollider : MonoBehaviour
{
Vector3 Origin;
public AudioClip SbHit;
public int minusScore = -5;
public ScoreManager scoremanager;
//Detect collision w/player, play audio, destroy sb and minus points
void OnCollisionEnter(Collision collision)
{
GetComponent<SteamVR_Camera>();
if (collision.gameObject.tag == "MainCamera")
{
Debug.Log("Hit by snowball");
Origin = new Vector3(0, 0, 0);
AudioSource.PlayClipAtPoint(SbHit, Origin);
Destroy(gameObject);
scoremanager.AddPoints(minusScore);
}
}
}