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 08, 2018 at 12:59 AM · unity 5variableresetvalue

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
Add comment · Show 1
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
avatar image Vicarian · May 08, 2018 at 01:12 AM 0
Share

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?

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

225 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 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

Variable being reset to default after being set 1 Answer

Infinite value add 0 Answers

ui onclick handler parameter to variable gets reset 1 Answer

Unity stop working after running this code, help!!!!!!!! (decremented of for varible) 3 Answers

explain me the script 0 Answers


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