PLEASE HELP! Increase slider when player collides with object

I am trying to get a slider to increase when the player collides with a object in the game world.
This is the code what i have so far on the game object.

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

public class oxygenp : MonoBehaviour
{

    void OnTriggerEnter(Collider col)
    {

        if (col.tag == "Player")
        {


            oxygen = oxygen + 700;


            if (oxygen > 1000)
            {
                oxygen = 1000;
            }


        }
    }
}

this is the code for the slider, it is like a timer, when it reachers 0 its game over.

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

public class oxygenscript : MonoBehaviour
{

    public Slider oxygenbar;
    public float oxygen;
    public float maxhealth = 1000;


    void Start()
    {
        oxygen = maxhealth;
        oxygenbar = GetComponent<Slider>();
        oxygenbar.maxValue = maxhealth;
        oxygenbar.value = oxygen;
    }
    void Update()
    {
        oxygen -= 0.1f;
        oxygenbar.value = oxygen;


        if (oxygen <= 0)
        {
            GameManager.Instance.setGameOver();

            Time.timeScale = 0;
        }




    }


}

Please post code using CODE tags so people can easily read it, and state what problem you’re running into with what you’re trying to do.

First, use code tags when you post code, it makes it much easier to read and to talk about.

So the main thing you need here is basically to connect the two, because right now they can’t see the same “oxygen” variable. You seem to already have a singleton, GameManager, which you should probably use for this. Put the “oxygen” variable on that script, then both of your scripts here will be able to see and modify GameManager.instance.oxygen.

2 Likes

sorry i’m new to ths i didnt know

i added GameManager.Instance.oxygen(); but it says
'Non-invocable member ‘GameManager.oxygen’ cannot be used like a method ’

It’s not a method, it’s a variable. Just use that exactly like you’re using “oxygen” now - add to it, set it, subtract from it, etc. No parentheses.

Did you adjusted slider correct
I think you forget to wrote a “f” after numbers because it is float