Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Picking up a MedKit when health is full

Discussion in 'Scripting' started by greeenlaser, Feb 10, 2019.

  1. greeenlaser

    greeenlaser

    Joined:
    Oct 5, 2018
    Posts:
    6
    I've run into a small issue i cant figure out how to solve, basically i want to pick up a medkit but whenever i step on the medkit in game when my health is already at 10, i get two debug messages saying i picked up a medkit and i cant pick up any more medkits

    How do i show the second message only when i want to pick up a medkit but my health is already full and how do i make it so that the medkit won't dissapear if my health is full?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class GlobalHealth : MonoBehaviour
    8. {
    9.     public static int PlayerHealth = 10;
    10.     public int InternalHealth;
    11.     public GameObject HealthDisplay;
    12.  
    13.     private void Update()
    14.     {
    15.         InternalHealth = PlayerHealth;
    16.         HealthDisplay.GetComponent<Text>().text = "Health:" + PlayerHealth;
    17.  
    18.         if (PlayerHealth > 10)
    19.         {
    20.             PlayerHealth = 10;
    21.             Debug.Log("Your Health is already full!");
    22.         }
    23.  
    24.         if (PlayerHealth < 0)
    25.         {
    26.             SceneManager.LoadScene(1);
    27.         }
    28.  
    29.         if (PlayerHealth == 0)
    30.         {
    31.             SceneManager.LoadScene(1);
    32.         }
    33.     }
    34. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MedKitPickup : MonoBehaviour
    6. {
    7.     public GameObject MedKit;
    8.  
    9.     private void OnTriggerEnter(Collider other)
    10.     {
    11.         if (GlobalHealth.PlayerHealth > 10)
    12.         {
    13.             Debug.Log("You can't pick up any more Small MedKits!");
    14.         }
    15.  
    16.         else
    17.         {
    18.             GlobalHealth.PlayerHealth += 1;
    19.             Debug.Log("You picked up a Small MedKit!");
    20.             MedKit.SetActive(false);
    21.         }
    22.     }
    23. }
     

    Attached Files:

    Last edited: Feb 10, 2019
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    368
    Your checking if health is BIGGER than 10, but it is exactly 10, therefore it gets picked up. Check bigger or equal to:

    Code (CSharp):
    1.       if (GlobalHealth.PlayerHealth >= 10)
    2.         {
    3.             Debug.Log("You can't pick up any more Small MedKits!");
    4.         }


    You can also combine these:

    1. Code (CSharp):
      1.         if (PlayerHealth < 0)
      2.         {
      3.             SceneManager.LoadScene(1);
      4.         }
      5.         if (PlayerHealth == 0)
      6.         {
      7.             SceneManager.LoadScene(1);
      8.         }
      Into:
      Code (CSharp):
      1.  if (PlayerHealth <= 0)
      2.         {
      3.             SceneManager.LoadScene(1);
      4.         }
     
    greeenlaser likes this.
  3. greeenlaser

    greeenlaser

    Joined:
    Oct 5, 2018
    Posts:
    6
    Thanks for the quick response and it worked :)