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. Dismiss Notice

Is it possible to use two OnTriggerEnter in one script?

Discussion in 'Scripting' started by Felipe_Schluepmann, Jan 16, 2021.

  1. Felipe_Schluepmann

    Felipe_Schluepmann

    Joined:
    Jan 12, 2021
    Posts:
    14
    I'm trying to make a health system script in which when the player collides with objects assigned with tags such as enemies the player loses health and when he collides with objects tagged HealthUp he gains one more life however when coding this an error occurs saying that I cant have two triggers on the same script, how could I rewrite it so that I could fix this problem?

    Script bellow if someone wants to understand it better:
    Code (CSharp):
    1. using System.Collections;using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class Health : MonoBehaviour
    6. {
    7.     [SerializeField] private Transform player;
    8.     [SerializeField] private Transform respawnPoint;
    9.  
    10.     public float health = 5f;
    11.     public float zeroHearts = 0f;
    12.  
    13.     public Text lifeText;
    14.  
    15.  
    16.     private void Update()
    17.     {
    18.  
    19.         lifeText.text = health.ToString();
    20.  
    21.         if (health <= 0)
    22.         {
    23.             health = zeroHearts;
    24.             FindObjectOfType<GameManager>().EndGame();
    25.         }
    26.  
    27.     }
    28.  
    29.     private void OnTriggerEnter(Collider col)
    30.     {
    31.         if (col.transform.tag == "HealthUp")
    32.         {
    33.             HealthUp(1);
    34.         }
    35.     }
    36.  
    37.     private void OnTriggerEnter(Collider col)
    38.     {
    39.         if (col.transform.tag == "Enemies")
    40.         {
    41.             TakeDamage(1);
    42.         }
    43.     }
    44.  
    45.     void TakeDamage(int damage)
    46.     {
    47.         health -= damage;
    48.     }
    49.  
    50.     void HealthUp(int heal)
    51.     {
    52.         health += heal;
    53.     }
    54. }
    55.  
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You can just do this check on the same function, do you expect them not to be called depending on the collider or something?
    Code (CSharp):
    1.     private void OnTriggerEnter(Collider col)
    2.     {
    3.         if (col.transform.tag == "HealthUp")
    4.         {
    5.             HealthUp(1);
    6.         }
    7.         else if (col.transform.tag == "Enemies")
    8.         {
    9.             TakeDamage(1);
    10.         }
    11.     }
     
    syedjawadakhtar likes this.
  3. Felipe_Schluepmann

    Felipe_Schluepmann

    Joined:
    Jan 12, 2021
    Posts:
    14
    This works perfectly for what I wanted to do thank you so much!