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

Question Code after OnTriggerEnter not working

Discussion in 'Editor & General Support' started by Parciwal, Jun 17, 2023.

  1. Parciwal

    Parciwal

    Joined:
    Apr 12, 2023
    Posts:
    2
    I have mad a void for OnTriggerEnter to let the Player die if it touches a thing. The weird thing is, I get back the Log but the actual thing it should do doesn't get executed. And weirder still, in the Fixed Update Void, it gets executed perfectly.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Die : MonoBehaviour
    6. {
    7.     public float Grenze;
    8.     public Vector3 Startposition;
    9.     void Start()
    10.     {
    11.       Startposition = transform.position;
    12.     }
    13.     private void OnTriggerEnter(Collider other)
    14.     {
    15.         Debug.Log("Stufe 1"); //working
    16.         if (other.gameObject.CompareTag("Death"))
    17.         {
    18.          StartCoroutine(Death()); //Not working
    19.          Debug.Log("Stufe 2"); //working
    20.         }
    21.     }
    22.     void FixedUpdate()
    23.     {
    24.       if(transform.position.y < Grenze)
    25.       {
    26.        StartCoroutine(Death()); //working
    27.       }
    28.     }
    29.     IEnumerator Death()
    30.     {
    31.         transform.position = Startposition; //working
    32.         yield return null;
    33.     }
    34. }
    35.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Probably because the if condition is failing, probably because the other game object doesn't have a "Death" tag.

    Try Debug.Logging the tag of the game object firing the trigger. It's a lot more useful to debug actual values rather than just strings to say something has happened.