How Can I Reset Values?(Unity3D)
Hi I"m using using Global,Local codes for show Health, Score and Timer Those showing right values when i start a game but there is a problem when i restart a game I'm using code below, for make a game restart.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour {
private GlobalInformation gi;
private int level;
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void QuitGame()
{
Debug.Log("QUIT A GAME");
Application.Quit();
}
public void ReturnGame()
{
Application.LoadLevel(0);
}
}
and I'm using "return Game" for restart game.
Here are my Global and Local codes 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);
}
}
}
and
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());
}
}
Please tell me what i have to put more info or something else.. Thank you
Comment
I don't see any code that actually changes the health and score values in GlobalInformation. The only thing that will change with these files is the time, which will increment ins$$anonymous$$d of decrement. Are these the only scripts in your project?