C
C#15mo ago
Captain BBQ

❔ (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(":)");
}
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(":)");
}
}
}
}
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();
}
}
}
}
}
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();
}
}
}
}
}
6 Replies
Angius
Angius15mo ago
OnCollisionEnter() is good, but it should not be a local function It should be a class member https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html
Captain BBQ
Captain BBQOP15mo ago
That makes a little more sense to me, it felt wrong writing it like that. The problem is I don’t know how to implement it as a class member 💀
Angius
Angius15mo ago
Like any normal method It goes into the class, not into another method
Captain BBQ
Captain BBQOP15mo ago
Am tired so I’ll try that more tomorrow, I did have it as a separate class earlier just didn’t know how to reference it into interact() without getting an error. Sorry I’m newer to coding so I don’t know what all the terms mean, got a little confused w/ local function and class member I have a better idea on how to follow through with the code though, thanks.
cap5lut
cap5lut15mo ago
Interact() is a class member of ObjPlace OnCollisionEnter(Collision) is a local function of Interact() so to make it OnCollisionEnter(Collision) a class member as well it has to be on the same level nesting/next to the Interact() method, not within in
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server