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!