Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Jun1643 · May 05, 2018 at 09:42 AM · timerscoreresetvalueshealth

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

142 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Timer instantly sets to zero 1 Answer

How to regenerate player's health after timer countdown is done. 1 Answer

Resetting Power up bar 0 Answers

How to add one score every second to scoremanager c# 1 Answer

How to save progress? and not delete values on load 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges