Search Unity

Object collision detection

Discussion in 'Scripting' started by David_AG, Jun 18, 2019.

  1. David_AG

    David_AG

    Joined:
    Jun 17, 2019
    Posts:
    2
    I am trying to get a block (Player) to be destroyed when it touches a wall. The script is attached to a wall game object, so it checks if the player has collided with the wall every frame. I have looked around and tried different approaches but to no luck. I'm still quite new and i'm shure that i'm just missing something simple.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class Wall_collision : MonoBehaviour
    6.     {
    7.  
    8.         void OnTriggerEnter(Collider collider)
    9.         {
    10.             if (collider.gameObject.CompareTag("Player"))
    11.             {
    12.                 Debug.Log("I WORK"); //Does not
    13.             }
    14.         }
    15.     }
    16.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    If you're using
    OnTriggerEnter
    , make sure the player's collider has "IsTrigger" checked. If you do not want to use a trigger collider on the player, then use OnCollisionEnter instead.

    In both cases, make sure the player object is actually tagged as "Player".
     
  3. David_AG

    David_AG

    Joined:
    Jun 17, 2019
    Posts:
    2
    Thank you! that fixed it, I knew it was something simple