How can i Reset Health,Score,Time???
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class LocalInformation : MonoBehaviour {
/* Reference to GlobalInformation script */
private GlobalInformation gi;
/* Values used for testing */
public int health; /* Value to change the global variable to; Set through the Inspector */
private float startTime;
public int score;
void Start () {
gi = GlobalInformation.gi; /* Set gi to the static reference of gi in the GlobalInformation script */
health = gi.gethealth(); /* Set oldTestValue to the current global variable before changing it */
startTime = (float)gi.getTimer();
InvokeRepeating("countdown", startTime, 1.0f);
score = gi.getscore();
}
void Update() {
/* Loads scenes on key press of 1, 2 or 3 at the top of the keyboard */
/* Used to test the changing of the global variable */
if (Input.GetKeyDown(KeyCode.Alpha1)) {
SceneManager.LoadScene(0);
}
if (Input.GetKeyDown(KeyCode.Alpha2)) {
SceneManager.LoadScene(1);
}
if (Input.GetKeyDown(KeyCode.Alpha3)) {
SceneManager.LoadScene(2);
}
if (health < 0) {
health = 0;
gi.setHealth(100);
gi.addtimer(100);
gi.NextLevelButton (4);
}
}
public void countdown()
{
gi.addtimer (-1);
}
void OnGUI() {
/* Display the old and new global variables */
GUI.Label(new Rect(10,10,200,30), "Health: " + gi.gethealth());
GUI.Label(new Rect(10,30,200,30), "Score: " + gi.getscore());
GUI.Label(new Rect(10,50,200,30), "Timer: " + gi.getTimer());
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GlobalInformation : MonoBehaviour {
/* Static reference to GlobalInformation script */
public static GlobalInformation gi;
public int globalscore;
public float timer;
public int phealth = 100;
public int maxHealth = 500;
private NetworkStartPosition[] spawnPoints;
/* Test global variable */
public int globalVariable;
/* Setter method for changing the global variable */
public void setHealth(int value) {
phealth = value;
}
/*Getter method for retrieving the global variable */
public int getGlobalVariable() {
return globalVariable;
}
public void addscore(int value)
{
globalscore += value;
}
public void addtimer(float value)
{
timer -= value;
}
public void adjusthealth(int value)
{
phealth += value;
if (phealth < 0) {
phealth = 0;
NextLevelButton(3);
}
if (phealth > maxHealth)
phealth = maxHealth;
if (maxHealth < 1)
maxHealth = 1;
}
public int getscore()
{
return globalscore;
}
public float getTimer()
{
return timer;
}
public int gethealth()
{
return phealth;
}
public void NextLevelButton(string levelName)
{
Debug.Log("index string activated");
SceneManager.LoadScene(levelName);
}
public void NextLevelButton(int level)
{
Debug.Log("index int activated");
if (level == 0)
{
Debug.Log("Level zero");
gi.setHealth(100);
gi.addtimer(100);
}
if (level == 3)
{
Debug.Log("Level four");
gi.setHealth(100);
gi.addtimer(100);
SceneManager.LoadScene(4);
}
}
void Awake() {
/* Sets the first instance of GlobalInformation that is loaded to the static GlobalInformation gi */
/* Any further instances are immediately destroyed */
/* Called in Awake() because Awake() runs before Start() */
if(gi == null) {
DontDestroyOnLoad(gameObject);
gi = this;
} else if(gi != this) {
Destroy(gameObject);
}
}
}
I'm using those codes for showing Health, Score and Time When I'm restart a game, those health,score, time shows me previous game's information. How should I reset those value when i restart a game??
I'm using this code for restart a game(Below) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Restart : MonoBehaviour
{
public void RestartGame()
{
Application.LoadLevel(0);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612162046im_/https://answers.unity.com/themes/thub/images/avi.jpg)