- Home /
Reset Score after game is finished?
I am trying to set the score of the game to zero every time the game is reset, I have no clue how to do this. I have two scripts that are for the score.
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> ();
}
void Update ()
{
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "Score: " + score;
}
}
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public int scoreValue = 1;
// Update is called once per frame
void OnMouseDown () {
ScoreManager.score += scoreValue;
}
}
The first one is to display the score. The second one adds points when the object is touched. After 30 seconds the game takes you to a menu and displays the score. Then there is a reset button that when is touched, I want it to reset the score. How do I do this.
Answer by hostShadows · Mar 30, 2015 at 05:47 AM
make an if statement in the Update that will set score = 0; when it's called (it should be called when the reset button is clicked) http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseUp.html this is what is used to detect clicking on an object
Answer by VioKyma · Mar 30, 2015 at 01:56 AM
If you are using Application.LoadLevel() on your Reset button, just set the score to 0 on Awake.
void Awake ()
{
// Set up the reference.
text = GetComponent <Text> ();
score = 0;
}
If not (or if you have set DontDestroyOnLoad on the ScoreManager), you will just need to get a reference to the ScoreManager and set the score to 0 on the Reset button click.
The reason I don't use void Awake is because it would only the show the score whilst you are playing it. I want the score to be shown after the game is done. ( I would like to note I am new to scripting ) I have made this script hoping it would just set the score to 0, but it doesn't, which is why I posted.
using UnityEngine;
using System.Collections;
public class Law : $$anonymous$$onoBehaviour {
void On$$anonymous$$ouseDown(){
Score$$anonymous$$anager.score = 0;
}
}
Okay, you will need to get the correct 'instance' of the Score$$anonymous$$anager script before you can set the score.
If the Score$$anonymous$$anager is a part of the same game object as Law, then you can use GetComponent():
void On$$anonymous$$ouseDown(){
Score$$anonymous$$anager score$$anonymous$$anager = GetComponent<Score$$anonymous$$anager>();
score$$anonymous$$anager.score = 0;
}
Otherwise, you will need to find the game object the Score$$anonymous$$anager is on, then get the component and set it (as above). If you need, I can help with getting the game object, just let me know how it's setup :)
The Score$$anonymous$$anager script is on a different object, the name of the object is ScoreText ( It is a UI text renamed, if that makes any difference ) The Law script is on a UI Button that has the script
#pragma strict
function LoadLevel(){
Application.LoadLevel("s");
}
I used an event trigger to have this script change the level.
Okay, not 100% on that last script you posted, but I think you can either do:
void On$$anonymous$$ouseDown(){
Score$$anonymous$$anager score$$anonymous$$anager =
GameObject.Find("ScoreText").GetComponent<Score$$anonymous$$anager>();
score$$anonymous$$anager.score = 0;
}
Or in the LoadLevel function:
function LoadLevel(){
Score$$anonymous$$anager score$$anonymous$$anager =
GameObject.Find("ScoreText").GetComponent<Score$$anonymous$$anager>();
score$$anonymous$$anager.score = 0;
Application.LoadLevel("s");
}
I decided to use the first one you mentioned. Using the exact same script this error pops up
Assets/Standard Assets ($$anonymous$$obile)/Textures/Law.cs(9,30): error CS0176: Static member `Score$$anonymous$$anager.score' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d
So I changed the script to
using UnityEngine;
using System.Collections;
public class Law : $$anonymous$$onoBehaviour {
void On$$anonymous$$ouseDown(){
GameObject.Find("ScoreText").GetComponent<Score$$anonymous$$anager>();
Score$$anonymous$$anager.score = 0;
}
}
But it doesn't set the score to 0