What I want is for when the player starts off inside a certain location it’s oxygen bar is full. Then when it exits the location the oxygen bar decreases, and when it returns to that location the oxygen bar increases again. However the problem I’m currently having is that when the player enters or exits, it remembers the value from the last time it was there- instead of increasing or decreasing from it’s current value. What am I doing wrong? Any help would be really appreciated. I’m doing this with 3 scripts:
Oxygen Decreases:
public class Oxygen : MonoBehaviour {
public float time;
private Slider oxygenSlider;
private GameObject oxygen;
public Slider oxygenAmount;
public float timeRestore;
// Use this for initialization
void Awake()
{
float timeRestore = gameObject.GetComponent<Oxygen>().timeRestore;
}
void Start () {
oxygenSlider = oxygen.GetComponent<Slider>();
oxygenSlider.value = timeRestore;
oxygenAmount.maxValue = time;
oxygenAmount.minValue = 0f;
}
// Update is called once per frame
void Update () {
time -= timeRestore - Time.deltaTime;
oxygenAmount.value = time;
}
}
Oxygen Increases:
public class OxygenRestore : MonoBehaviour
{
public float time;
private Slider oxygenSlider;
private GameObject oxygen;
public Slider oxygenAmount;
public float timeRestore;
// Use this for initialization
void Awake()
{
float time = gameObject.GetComponent<Oxygen>().time;
}
void Start()
{ oxygenSlider = oxygen.GetComponent<Slider>();
oxygenSlider.value = time;
oxygenAmount.maxValue = 100f;
oxygenAmount.minValue = timeRestore;
}
// Update is called once per frame
void Update()
{
timeRestore += time + Time.deltaTime;
oxygenAmount.value = timeRestore;
}
}
OnTrigger Enter/Stay/Exit
public class InsideShipGravity : MonoBehaviour
{
public GameObject oxygen;
public GameObject player;
void OnTriggerEnter2D(Collider2D Collider)
{
if (Collider.gameObject.tag == "Player")
{
oxygen.GetComponent<Oxygen>().enabled = false;
oxygen.GetComponent<OxygenRestore>().enabled = true;
}
}
void OnTriggerStay2D(Collider2D Collider)
{
if (Collider.gameObject.tag == "Player")
{
if (Collider.gameObject.tag == "Player")
{
oxygen.GetComponent<Oxygen>().enabled = false;
oxygen.GetComponent<OxygenRestore>().enabled = true;
}
}
void OnTriggerExit2D(Collider2D Collider)
{
if (Collider.gameObject.tag == "Player")
{
if (Collider.gameObject.tag == "Player")
{
oxygen.GetComponent<Oxygen>().enabled = true;
oxygen.GetComponent<OxygenRestore>().enabled = false;
}
}
I’m also getting the error- NullReferenceException: Object reference not set to an instance of an object.