Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Sonen3 · Oct 25, 2011 at 06:04 PM · c#sceneplayerprefschangehealth

Health script, save health in between scenes. PlayerPrefs.

Hi I have made a simple health script, using Unity's GUI to create a health bar which decreases as you loose health. I've also added a button which makes you loose 10 health each time you press it.

Now I'm trying to script so that your current health stays the way it was last scene, when you change scene. I have tried using PlayerPrefs, and it does work since the current health is the same when you change scene. Though if you quit the game and re-enter you still have the same amount of hp you had last time you played. I want it to go back to maximum health at every start-up, though not when the next scene starts.

using UnityEngine; using System.Collections;

public class healthScript : MonoBehaviour {

public int maxHealth = 100; // Minimum health is 0.0f (dead)

public int currentHealth = 100; // Players current health

public float healthBarLenght;

public Texture healthBartexture;

 void Awake (){
     
 PlayerPrefs.SetInt("healthP",maxHealth);
 }
 
 // Use this for initialization
 void Start () {
     currentHealth = PlayerPrefs.GetInt("healthP");
     healthBarLenght = Screen.width / 2;
 }

 
 // Update is called once per frame
 void Update () {
     
     adjustCurrentHealth(0);
     if(Input.GetKeyDown(KeyCode.O))
     {
     currentHealth = currentHealth - 10;
     SaveHealth();
     }
 
 }
 void OnGUI (){        
     
     GUI.Box(new Rect(10, 10, healthBarLenght, 20), currentHealth + "/" + maxHealth);    
     
     if (currentHealth == 0)
     {
     GUI.Box(new Rect(0,0,Screen.width,Screen.height), "Game Over");    
     Time.timeScale = 0.0f;
     }
 }
 public void adjustCurrentHealth(int adj)
 {
     currentHealth += adj;
     
     if (currentHealth < 0)
     {
         currentHealth = 0;
     }
     if (currentHealth > maxHealth)
     {
         currentHealth = maxHealth;
     }
     if (maxHealth < 1)
     {
         maxHealth = 1;
     }
     
     healthBarLenght = (Screen.width / 2) * (currentHealth / (float)maxHealth);
 }
 void SaveHealth (){
 PlayerPrefs.SetInt("healthP",currentHealth);
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by save · Oct 25, 2011 at 06:09 PM

Protect it by using DontDestroyOnLoad() between scenes. Only save health when saving the state of progress.

Comment
Add comment · Share
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
0

Answer by green_core · Oct 26, 2011 at 08:09 AM

Also you can just use static variable to save health.

Comment
Add comment · Show 2 · Share
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 Sonen3 · Oct 26, 2011 at 12:50 PM 0
Share

How do i do that? :-P I am really new to all this

avatar image timsk · Oct 26, 2011 at 01:24 PM 0
Share

change

 public int maxHealth = 100;

to

 static int maxHealth = 100;

think of static variables as "global". 1 variable affects all instances of an object / member. Therefore, it makes sense that there can only be 1 instance of the variable.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I'm trying to set a high score but I can't display it in another scene? 2 Answers

How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer

Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer

How to write If the player don't play the game for 24 hours, the game is over. 1 Answer

How do I load a new scene based on current scene? 3 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