❔ (Unity C#) Dont know how to implement the OnCollisionEnter function w/ this code

Im referencing a template code for interaction and I want the Debug.Log to appear if Im interacting with the object and collision is happening simultaneously.

If I do this below it works fine

public void Interact()
{
        Debug.Log(":)");
}



I just dont know how to implement the collision, below is my best attempt. (No errors btw)


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

public class ObjPlace : MonoBehaviour, IInteractable
{
    public void Interact()
    {
        void OnCollisionEnter(Collision collision)
        {
            if (collision.gameObject.tag == "canPickUp")
            {
                Debug.Log(":)");
            }
        }
    }
}



Im not sure if this will help but heres the code im referencing below


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

interface IInteractable
{
    public void Interact();
}

public class Interactor : MonoBehaviour
{
    public Transform InteractorSource;
    public float InteractRange;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            Ray r = new Ray(InteractorSource.position, InteractorSource.forward);
            if (Physics.Raycast(r, out RaycastHit hitInfo, InteractRange))
            {
                if (hitInfo.collider.gameObject.TryGetComponent(out IInteractable interactObj))
                {
                    interactObj.Interact();
                }
            }
        }
    }
}
Was this page helpful?