2
votes

I'm trying to do collision for a 2D character and a collider object. I have defined the OnTriggerEnter function to display a message in the debugger when a trigger is entered. The character is the "CharacterRobotBoy" asset from the Unity standard assets package (contains a BoxCollider2D) and I want it to collide with another object with attached BoxCollider2D, set as a trigger. I have trigger ticked on the second object.

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

public class PickUpCheck : MonoBehaviour {


    private int pickUpCount;

    // Use this for initialization
    void Start () {
        pickUpCount = 0;
    }


    void OnTriggerEnter(Collider collider)
    {
       // if (collider.gameObject.name == "RobotBoy")
       //     pickUpCount++;
        Debug.Log("PickUp " + pickUpCount);
    }

    // Update is called once per frame

    void Update () {

    }
}

I've tried attaching the script to both the character and the other object but can't seem to register collisions with the trigger.

/edit - I've read there is or was an OnTriggerEnter2D. I've tried calling this but it's not recognized in Visual Studio. Not sure if it still exists or I'm doing something wrong?

/edit - Switched code to -

void OnTriggerEnter2D(Collider2D collider)
{
   // if (collider.gameObject.name == "RobotBoy")
   //     pickUpCount++;
    Debug.Log("PickUp " + pickUpCount);
}

But still no luck...

Pick up object

/edit - @Eddge Have set a common layer for both the character and pickup object, though I think collisions should still occur with no layers set?

Collision Matrix

This answer was informative, but I made sure I have the described components in my set up already - colliders on both objects, rigidbody present on one of the objects and one set as trigger.

/edit Solved! - Ok, so I cleaned up the debug (thanks to @Eddge for the suggestion). I moved the script to the character and noticed that collisions were occurring but not with the pickup object.

I switched the code in the OnTriggerEnter2D to output the name of the collided objects and this helped me get a clearer idea of what was going on:

void OnTriggerEnter2D(Collider2D collider)
{
    Debug.Log(collider.gameObject.name);
}

Turns out the problem was with the BoxCollider2D in the prefab I modified to be the pickup. I rebuilt the game object and this solved the problem.

2
Does your character have a rigidbody component? - Draco18s no longer trusts SE
Yes, a rigidbody2D - G.G.
Look at Carlos's answer and then see these answers: stackoverflow.com/questions/49178539/… stackoverflow.com/questions/48304293/… In regards to the second one OnTriggerEnter2D only works with 2d colliders and OnTriggerEnter only work with 3d colliders. - AresCaelum
@Eddge sounds like a duplicate! Flag or vote as such. - Draco18s no longer trusts SE
Thanks, new info for me in both of those links. But still not getting a response from the script on collision. To clarify, the character object has both a Box Collider 2D and a Rigidbody 2D. The pickup object only had a Box Collider 2D, but I've since added and tried the script with a Rigidbody2D also. Still no response... - G.G.

2 Answers

2
votes

If you're working on a 2D game, you must use OnTriggerEnter2D(Collider2D other). Currently, you're using OnTriggerEnter which will only register 3D collisions. Also, make sure you're passing a Collider2D as the function's parameter.

So change your code to this:

 void OnTriggerEnter2D(Collider2D collider)
{
   // if (collider.gameObject.name == "RobotBoy")
   //     pickUpCount++;
    Debug.Log("PickUp " + pickUpCount);
}

Check out the Unity API docs

0
votes

So I made sure I had the correct components present for a collision to occur (detailed here). Turns out my first problem was using OnCollisionEnter instead of OnCollisionEnter2D (note that you must use the Collider2D parameter with this). Thanks to Carlos for clarifying this.

But alas, the collisions still would not occur...

I then checked to make sure layers weren't a problem. Layers can be used to organize collision interactivity between objects. More info here and here.

Finally, I switched the script from the pickup to the player controller and found it was able to collide with objects other than the pickup with a debug log of the name of the collided object.

I recreated the game object - object, BoxCollider2D and script and the collisions started working.