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;
}
}
}
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.
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.