- Home /
Scoring System between two different scene?
Hello. I am trying to figure out why the ScoreManager doesn't work for my game. I am using the Scripts that were provided by Unity Tutorials which were
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static int score; // The player's score.
Text text; // Reference to the Text component.
void Awake ()
{
// Set up the reference.
text = GetComponent <Text> ();
// Reset the score.
score = 0;
}
void Update ()
{
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "Score: " + score;
}
}
And of course to update the score this
using UnityEngine;
using System.Collections;
public class Expands : MonoBehaviour {
public Transform parent;
public float expandAmount = 2f;
public float localPosition = 1f;
public int scoreValue = 1;
void OnMouseDown()
{
parent.localScale *=expandAmount;
for(int i =0;i<parent.childCount;i++)
{
parent.GetChild(i).localScale = parent.GetChild(i).localScale/expandAmount;
parent.GetChild(i).localPosition += new Vector3(0, 0, -1f);//Added a '+'
ScoreManager.score += scoreValue;
}
}
}
When the game is finished, it takes you to a different scene which would be sort of like the Menu you see at the end of a mobile game ( The game is for Mobile and is 2D ) The problem I have is that the Score does not get updated and stays at zero. If anyone knows the solution, please do say. All help is appreciated.
Answer by revolute · Mar 23, 2015 at 02:35 AM
The easiest way to do this is make static class.
For instance, one can define a class public static and all its variables static.
However, as this can only hold static variables and functions, you may need to split classes.
Static classes cannot and does not need to be attached to a gameobject. It will stay alive.
Answer by Socrates · Mar 23, 2015 at 02:36 AM
For clarification: Does the score get updated while the game is being played, then when you move to the menu you revert to zero score?
If so, are you using Object.DontDestroyOnLoad() on whichever object is holding a copy of your ScoreManager script?
Also make sure that you are not creating a new instance of ScoreManager in the new menu scene. Your Awake() function is resetting the score to zero, so every time you create a new instance of this class, you are resetting your score to zero.
Your answer
Follow this Question
Related Questions
Why won't the Score reset? 2 Answers
Carry over texts from one scene to another? 1 Answer
Expand object on touch? 1 Answer
Displaying the score after a reset. 2 Answers
While the Parent expands, the children get smaller. Problem 2 Answers