Search Unity

PLEASE HELP! Increase slider when player collides with object

Discussion in 'Scripting' started by Gabimela, Jan 15, 2020.

  1. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class oxygenp : MonoBehaviour
    7. {
    8.  
    9.     void OnTriggerEnter(Collider col)
    10.     {
    11.  
    12.         if (col.tag == "Player")
    13.         {
    14.  
    15.  
    16.             oxygen = oxygen + 700;
    17.  
    18.  
    19.             if (oxygen > 1000)
    20.             {
    21.                 oxygen = 1000;
    22.             }
    23.  
    24.  
    25.         }
    26.     }
    27. }
    28.  

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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class oxygenscript : MonoBehaviour
    7. {
    8.  
    9.     public Slider oxygenbar;
    10.     public float oxygen;
    11.     public float maxhealth = 1000;
    12.  
    13.  
    14.     void Start()
    15.     {
    16.         oxygen = maxhealth;
    17.         oxygenbar = GetComponent<Slider>();
    18.         oxygenbar.maxValue = maxhealth;
    19.         oxygenbar.value = oxygen;
    20.     }
    21.     void Update()
    22.     {
    23.         oxygen -= 0.1f;
    24.         oxygenbar.value = oxygen;
    25.  
    26.  
    27.         if (oxygen <= 0)
    28.         {
    29.             GameManager.Instance.setGameOver();
    30.  
    31.             Time.timeScale = 0;
    32.         }
    33.  
    34.  
    35.  
    36.  
    37.     }
    38.  
    39.  
    40. }
     
    Last edited: Jan 15, 2020
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    First, use [code ]code tags[/code] 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.
     
    Joe-Censored and Gabimela like this.
  4. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23

    sorry i'm new to ths i didnt know
     
  5. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    i added
    Code (CSharp):
    1. GameManager.Instance.oxygen();
    but it says
    'Non-invocable member 'GameManager.oxygen' cannot be used like a method '
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  7. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    32
    Did you adjusted slider correct
    I think you forget to wrote a "f" after numbers because it is float