OnTriggerEnter not working

So I have a build system the creates a preview thats transform is equal to the raycast’s hit position. There is a snappable object that the preview can snap to which have colliders on them set to trigger mode. My preview object also has a collider on it but it’s not set to trigger. Both the preview and the snappable object have rigidbodies set to kinematic. Whenever my preview run’s into the snappable object, it should call the on trigger enter function, but it doesn’t. Here is my code. This doesn’t pertain to any of there courses.

Hi AstroneerKing,

Have you already tried to add Debug.Logs to analyse the problem further?

Hey Nina,

Sorry for the late response. Yes, I did use debug logs to analyze it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Preview : MonoBehaviour
{
    [SerializeField] private GameObject objectToBuild;

    private MeshRenderer meshRend;
    [SerializeField] private Material goodPreview;
    [SerializeField] private Material badPreview;

    private BuildSystem buildSystem;

    private bool isSnapped = false;
    [SerializeField]private bool isStarterBoat = false;

    [SerializeField] private List<string> snappableTags = new List<string>();
    
    private void Awake() 
    {
        buildSystem = GameObject.FindObjectOfType<BuildSystem>();
        meshRend = GetComponent<MeshRenderer>();
    }

    public void placePiece()
    {
        Instantiate (objectToBuild, transform.position, transform.rotation);
        Destroy(gameObject);
    }

    public void changeColour()
    {
        if (isSnapped)
        {
            meshRend.material = goodPreview;
        }
        else
        {
            meshRend.material = badPreview;
        }

        if (isStarterBoat)
        {
            meshRend.material = goodPreview;
            isSnapped = true;
        }
    }
    void onTriggerEnter(Collider other)
    {
        Debug.Log("Hit Something");
        for (int i = 0; i < snappableTags.Count; i++)
        {
            string currentTag = snappableTags[i];
            
            if(other.tag == currentTag)
            {
                Debug.Log("Hit Something Snappable");
                buildSystem.pauseBuild(true);
                transform.position = other.transform.position;
                isSnapped = true;
                changeColour();
            }
            
        }
    }
    void onTriggerExit(Collider other)
    {
         for (int i = 0; i < snappableTags.Count; i++)
        {
            string currentTag = snappableTags[i];

            if(other.tag == currentTag)
            {
                isSnapped = false;
                changeColour();
            }
            
        }
    }
    public bool getSnapped()
    {
        return isSnapped;
    }
}

And here is my other code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BuildSystem : MonoBehaviour
{
    [SerializeField] private GameObject floor;
    [SerializeField]Camera rayCam;
    [SerializeField] LayerMask layerM;
    private GameObject previewGameObject;
    private Preview previewScript;

    [SerializeField] private float snappingTolerance = 0.5f;

    private bool isBuilding = false;
    private bool ispauseBuild = false;

    void Update()
    {

        if(Input.GetKeyDown(KeyCode.E))
        {
            newBuild(floor);
            isBuilding = true;
        }

        if(Input.GetKeyDown(KeyCode.G))
        {
            cancleBuild();
        }

        if(Input.GetMouseButtonDown(0) && isBuilding)
        {
            if(previewScript.getSnapped())
            {
                createPiece();
            }
            else
            {
                Debug.Log("not snapped");
            }
        }

        if(isBuilding)
        {
            if(ispauseBuild)
            {
                float mouseX = Input.mousePosition.x;
                float mouseY = Input.mousePosition.y;

                if(Mathf.Abs(mouseX) >= snappingTolerance || Mathf.Abs(mouseY) >= snappingTolerance)
                {
                    ispauseBuild = false;
                }
            }
            else if(!ispauseBuild)
            {
                doBuildRay();
            }
        }
    }

    public void newBuild(GameObject go)
    {
        previewGameObject = Instantiate(go, Vector3.zero, Quaternion.Euler(-90,0,0));
        previewScript = previewGameObject.GetComponent<Preview>();
        previewScript.changeColour();
    }

    void cancleBuild()
    {
        Destroy(previewGameObject);
        previewGameObject = null;
        previewScript = null;
        isBuilding = false;
    }

    void createPiece()
    {
        previewScript.placePiece();
        previewGameObject = null;
        previewScript = null;
        isBuilding = false;
    }

    public void pauseBuild(bool val)
    {
        ispauseBuild = val;
        Debug.Log("oh snap!");
    }

    void doBuildRay()
    {
        Ray ray = rayCam.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        if(Physics.Raycast(ray, out hitInfo, 100f ,layerM))
        {
            Vector3 pos = new Vector3(hitInfo.point.x, hitInfo.point.y, hitInfo.point.z);
            previewGameObject.transform.position = pos;
        }

    }
}

Could you share the output, please? Did the messages appear in your console? If so, when? Did they appear when they were supposed to appear?

The message didn’t seem to appear at all which is telling me that there is not onTriggerEnter that is not even happening. So I was wondering what is causing the onTriggerEnter to not be triggering.

Maybe there is a typo in your code. C# is case-sensitive. Make sure you spelt OnTriggerEnter correctly.

I will certainly check if that’s the case and I will tell you if it worked. :+1:

OnTriggerEnter must have a Capital “o” or Unity will never call it. Also if both game objects are trigger volumes, no collision will occur.

Hope this helps!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms