Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

SendMessage/OnCollisionEnter issue

Discussion in 'Scripting' started by lallassu, Jun 15, 2016.

  1. lallassu

    lallassu

    Joined:
    Jun 15, 2016
    Posts:
    3
    Hi,

    I've got two meshcolliders where one of them has a rigidbody. I've implemented it all with a class that does not inherit from MonoBehaviour (class Chunk). My Chunk class has a GameObject which I instantiate for each instance of my Chunk class. Everything works fine and there are working collisions between meshes.

    However, now I want to detect if the objects collide using either "SendMessage" method on the GameObject or the OnCollisionEnter() function. But since I don't inherit from MonoBehaviour, this is not working.

    Any idea how I can get my callbacks working for collision detection using my approach?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    for OnCollisionEnter to work you'll need a script on the gameobject doing the collision. I.e. the "Chunk" instantiates the gameobject prefab which has the script component already attached to it (or you add it afterwards).

    Sounds like you've created something similar to scriptableObjects to be honest...
     
  3. lallassu

    lallassu

    Joined:
    Jun 15, 2016
    Posts:
    3
    I've only been working with Unity in couple of days so far. So could you please elaborate a bit on your answer? How can I attach a script to the gameobject? Do you mean creating a new class with MonoBehaviour inheritance and then add that as a component to my GameObject and this addition will have a OnCollisionEnter function?
     
  4. lallassu

    lallassu

    Joined:
    Jun 15, 2016
    Posts:
    3
    I managed to get it working just as you said. Just had to process your explanation a bit more :) Thanks!