Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Why do this simple collider trigger not work??

Discussion in 'Scripting' started by LetmeDwight, Feb 15, 2021.

  1. LetmeDwight

    LetmeDwight

    Joined:
    Apr 9, 2020
    Posts:
    125
    I just got this single script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Colliderblock : MonoBehaviour
    6. {
    7.     void OnTriggerEnter(Collider collider)
    8.     {
    9.         if (collider.gameObject.tag == "Player")
    10.         {
    11.             Debug.Log("Hello");
    12.         }
    13.     }
    14. }
    Why do nothing happen in the console, if I move the Player Cube into the collider trigger if the cube with this script?
    1.PNG 2.PNG
     
  2. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,229
    Add a rigidbody to your Player cube (turn off Gravity if you want to test in the scene editor)
     
    LetmeDwight likes this.
  3. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,229
    Here is a quick test script for you:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class test1 : MonoBehaviour
    4. {
    5.     private const string playerTag = "Player";
    6.  
    7.     private void Update()
    8.     {
    9.         transform.position = new Vector3(transform.position.x + 0.01f, transform.position.y, transform.position.z);
    10.     }
    11.  
    12.     private void OnTriggerEnter(Collider other)
    13.     {
    14.         // Use CompareTag to avoid Garbage Collection
    15.         if (other.gameObject.CompareTag(playerTag))
    16.         {
    17.             Debug.Log("Hello");
    18.         }
    19.     }
    20.  
    21.     private void OnTriggerStay(Collider other)
    22.     {
    23.         if (other.gameObject.CompareTag(playerTag))
    24.         {
    25.             Debug.Log("waving...");
    26.         }
    27.     }
    28. }
    Here I'm moving the other object (rather than the player) but you can see the result.
     
  4. LetmeDwight

    LetmeDwight

    Joined:
    Apr 9, 2020
    Posts:
    125
    wow that simple thing helped me a lot!
     
    sstrong likes this.