Hi there. I’m trying to execute a simple collision detection to create an underwater effect. I’ve successfully debugged collisions with scripts attached directly to game objects but once I try to incorporate a manager that detects all collisions on a specific layer (in this case Water), I seem to be unable to get any collision detection. Below is the script I’ve been using for my Underwater Manager game object.
using UnityEngine;
public class UnderwaterEffect : MonoBehaviour
{
[Header("Settings")]
public string waterLayerName = "Water"; // The name of the water layer
private void Start()
{
Debug.Log("[UnderwaterEffect] Script initialized and ready.");
// Validate the water layer
int waterLayer = LayerMask.NameToLayer(waterLayerName);
if (waterLayer == -1)
{
Debug.LogError($"[UnderwaterEffect] Layer '{waterLayerName}' does not exist! Please check your layers.");
}
else
{
Debug.Log($"[UnderwaterEffect] Water layer '{waterLayerName}' found with index {waterLayer}.");
}
}
private void OnTriggerEnter(Collider other)
{
Debug.Log($"[UnderwaterEffect] Trigger Entered by: {other.name}, Layer: {other.gameObject.layer} ({LayerMask.LayerToName(other.gameObject.layer)})");
// Check if the object's layer matches the water layer by name
if (LayerMask.LayerToName(other.gameObject.layer) == waterLayerName)
{
Debug.Log($"[UnderwaterEffect] Object '{other.name}' is in the water layer.");
}
else
{
Debug.Log($"[UnderwaterEffect] Object '{other.name}' is NOT in the water layer.");
}
}
private void OnTriggerExit(Collider other)
{
Debug.Log($"[UnderwaterEffect] Trigger Exited by: {other.name}, Layer: {other.gameObject.layer} ({LayerMask.LayerToName(other.gameObject.layer)})");
// Check if the object's layer matches the water layer by name
if (LayerMask.LayerToName(other.gameObject.layer) == waterLayerName)
{
Debug.Log($"[UnderwaterEffect] Object '{other.name}' exited the water layer.");
}
else
{
Debug.Log($"[UnderwaterEffect] Object '{other.name}' exited but is NOT in the water layer.");
}
}
}