- Home /
Saving an integer on scene reload?
I have a money script and a health script. When my health runs out, I don't want my money to FULLY run out, I just want the player to lose SOME of it. These are my scripts:
Money: using UnityEngine; using System.Collections;
public class MoneyAdd : MonoBehaviour {
int add_amount;
public GameObject Money_Item;
int remove_amount;
// Use this for initialization
void Start () {
add_amount = Random.Range (0,1000);
remove_amount = Random.Range (0, 5000);
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider other) {
if (other.tag == "Player") {
Money.PlayerMoney += add_amount;
GameObject.Destroy (Money_Item);
}
}
}
Health: using UnityEngine; using System.Collections;
public class health : MonoBehaviour {
public static int PlayerHealth;
// Use this for initialization
void Start () {
PlayerHealth = 100;
}
// Update is called once per frame
void Update () {
guiText.text = PlayerHealth.ToString ();
if (PlayerHealth > 100) {
PlayerHealth = 100;
}
if (PlayerHealth < 0) {
PlayerHealth = 0;
}
if (Input.GetButtonDown ("Fire")) {
if (armor.playerarmor <= 0) {
PlayerHealth -= 5;
}
}
if (PlayerHealth <= 0) {
Application.LoadLevel ("scene1");
}
}
}
I want the Money.PlayerMoney to be carried on (and then be subtracted by remove_amount). Money.PlayerMoney is in a script called (as you may be able to guess) Money. Here is that:
using UnityEngine;
using System.Collections;
public class Money : MonoBehaviour {
public static int PlayerMoney;
// Use this for initialization
void Start () {
PlayerMoney = 0;
}
// Update is called once per frame
void Update () {
}
}
Short and sweet. Then I have MoneyHandler. Here is that:
using UnityEngine;
using System.Collections;
public class MoneyHandler : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Money.PlayerMoney > 999999999) {
Money.PlayerMoney = 999999999;
}
if (Money.PlayerMoney < 0) {
Money.PlayerMoney = 0;
}
}
void OnGUI () {
guiText.text = "$" + Money.PlayerMoney.ToString ();
}
}
Now, can anyone help? I don't even mind using PlayerPrefs!
Saying DontDestroyOnLoad does not work on an integer makes no sense. If you are willing to use PlayerPrefs, then why don't you look at doc/tut on using PlayerPrefs and revise this question with something specific.
DontDestroyOnLoad requires an object, won't work on an integer.
Wait... How do you...? But.... $$anonymous$$ake it work on an int? AHH! Seriously, how do you make a gameobject an int?
Answer by getyour411 · Mar 25, 2014 at 11:12 PM
Of course, but the object can have any number of fields, properties, methods, functions, etc - - such as an int
PlayerPref.SetInt/GetInt is well documented - have you looked at it? https://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Floating Healthbar Appears when facing opposite direction of enemy 1 Answer
How can i modify a float in another script in C#? 1 Answer
Health Script/Targeting system c# 2 Answers