Oxygen Bar won't decrease (need help with tag detection and collision)

I am trying to decrease a slider amount when I collide with water. My code is listed below:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerOxygen : MonoBehaviour
{
    public Slider oxygenBar;
    public float oxygenAmount = 45.0f;
    private float currentOxygen;
    private bool isUnderwater = false;

    public delegate void OxygenEvent();
    public static event OxygenEvent OnNoOxygen;

    // Use this for initialization
    void Start ()
    {
        currentOxygen = oxygenAmount;
    }

    // Update is called once per frame
    void OnTriggerStay()
    {
        isUnderwater = true;
        currentOxygen -= Time.deltaTime;
        oxygenBar.value = currentOxygen/oxygenAmount;

        if(currentOxygen <= 0)
        {
            currentOxygen = 0;
           
            if(OnNoOxygen != null)
            {
                OnNoOxygen();
            }
        }
    }

    void Update()
    {
        if(!isUnderwater)
        {
            Debug.Log ("Not in water!");
            currentOxygen += Time.deltaTime;
            if(currentOxygen >= oxygenAmount)
            {
                currentOxygen = oxygenAmount;
            }
            oxygenBar.value = currentOxygen/oxygenAmount;
        }
        isUnderwater = false;
    }
}

I have no tag detection, but I am unsure how to add this. What I am trying to do is have the slider that is attached in the float decrease when the player (which the GameObject is attached to) collides with the water tag. Then it will decrease by 1 every second that the player is underwater, until the player resurfaces. Can anyone please help, and revise my code in any way?

Is your collider setup as a trigger (one of them), as you’re using OnTriggerStay().

Another way to do this could be to use OnTriggerEnter/Exit, and set a bool. You could check this bool (say ‘isUnderWater’) in Update, like you are now, but both for in and out of water.

As for comparing a tag…if the script is on the player:

if(other.CompareTag("Water")) {
   // code here
 }

Since my code snippet is small, here is the documentation: Unity - Scripting API: Component.CompareTag

I’m not using any colliders at all, I am relying on the script for that, but I am unsure how to modify my script to fit what I am trying to do with it. I am relatively new to scripting, so I don’t have too much knowledge on what I am doing.

I greatly revised my code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerOxygen : MonoBehaviour
{
    public Slider oxygenBar;
    public float oxygenAmount = 45.0f;
    public float currentOxygen;
    public bool isUnderwater = false;

    public delegate void OxygenEvent();
    public static event OxygenEvent OnNoOxygen;

    // Use this for initialization
    void Start ()
    {
        currentOxygen = oxygenAmount;
    }

    // Update is called once per frame
    void OnCollisionEnter(Collision collisionInfo)
    {
        if (collisionInfo.collider.tag == "Water")
        {
            isUnderwater = true;
            currentOxygen -= Time.deltaTime;
            oxygenBar.value = currentOxygen / oxygenAmount;
        }

        if(currentOxygen <= 0)
        {
            currentOxygen = 0;
           
            if(OnNoOxygen != null)
            {
                OnNoOxygen();
            }
        }
    }

    void Update()
    {
        if(!isUnderwater)
        {
            Debug.Log ("Not in water!");
            currentOxygen += Time.deltaTime;
            if(currentOxygen >= oxygenAmount)
            {
                currentOxygen = oxygenAmount;
            }
            oxygenBar.value = currentOxygen/oxygenAmount;
        }
        isUnderwater = false;
    }
}

The code is functioning, but It doesn’t change my oxygen amount or change the slider value. Please help!

You need a collider and rigidbody to get that message callback “OnTriggerEnter”.
Both objects interacting need a collider and at least 1 needs a rigidbody**.
If you scroll down on this page, you can view valid combinations for trigger/collisions: Unity - Manual: Introduction to collision

You may also need the OnTriggerExit method to disable the bool.

You must check this bool in Update, in the new code, and then update the slider & value there.
OnTriggerEnter is only called on the first frame (assuming it’s even working… or when it is setup properly).

The ‘CompareTag’ I wrote before is better to use than ‘==’.

You’ve put ‘isUnderWater = false’ in Update() ; this would be better inside OnTriggerExit, for example.

I hope that helps. Out of curiosity, have you already followed and completed some of the tutorials in the Learn section on this website, before beginning your own project? They are useful, and will help you get acquainted with Unity and practice using the engine, as well as scripting.

I have watched and learned from brackeys, but I didn’t watch any of the tutorials. Thanks BTW!

I’d suggest that you try to work from the easiest tutorials and work your way up. When you follow along, write all of the code - don’t just copy & paste it - so you get used to it. Try to make sure it’s all making sense to you.
Build up slowly, and practice. Try to be creative and add 1 or more small modifications and continue like that.

The most basic things are going to be found everywhere in what you do, and will always be with you… therefore, the better/stronger your foundation is, the better you’ll be going forward! :slight_smile:

Anyways… good luck and have fun =)