- Home /
col.gameObject.layer is not working
As you can see in this script, I try to destroy the rigibody2D of a gameObject with a BoxCollider that is set to Trigger when the triggered collider collides with a gameObject with a specific layer. But it seems that it is not working...
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Photon.Pun;
 
 public class ChestScript : MonoBehaviour
 {
     [SerializeField] Rigidbody2D rb;
 
     public LayerMask groundLayer, PlayerLayer;
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.gameObject.layer == groundLayer)
         {
             Destroy(rb);
         }
         else if(col.gameObject.layer == PlayerLayer)
         {
             ChangeWeapon();
         }
     }
 
     void ChangeWeapon()
     {
         //Changing weapon
 
         Destroy(gameObject);
     }
 }
I tried with TriggerStay2D too... When I debugged the script, it seems that the Trigger function is working but what's not working is in the if (and else if) statement.
It seems that it is working if I use ints to specify the layer and not the LayerMask Variable
Answer by qsp18 · Aug 20, 2021 at 05:56 PM
Layer is a number and Layermask is .... like a number, but it contains multiple boolean informations that u can read out with bitshift. If u dont want to go too deep into the subject, just test col.gameObject.layer == 3 (if ur layer has number 3)
U can see the layernumber when clicking on layer in the inspector. -> change in line 10 the "LayerMask" to an integer
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                