OnTriggerEnter2D

I have a 2d top-down game, the player has 4 2D box colliders around him (empty gameobjects wich are child of the player) (one in each direction) wich each has a script with this code:

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

public class CheckWallRight : MonoBehaviour
{

    public PlayerController pc;

    void Start()
    {
        pc = GetComponent<PlayerController> ();
    }

    void Update ()
    {

    }

    void OnTriggerEnter2d(Collider2D other)
    {
        if (other.gameObject.tag == "Collision")
        {
            pc.canMoveRight = false;
        }
    }

}

(obviously the others have “left”, “up” and “down” instead of “right”)

I have some walls in my level wich also has 2D box colliders and a wall tag. I have made it so the player can’t move in a certain direction if he bool for that direction is not true (if canMoveLeft is false, he can’t move left) but I can’t seem to get the wallChecks working… does anyone know why? (I hope I gave you enough information)

Typo in the trigger method: ‘2d’ needs to be ‘2D’, just like you’ve written it in the thread title.
Fix it and report back whether it’s working then. Otherwise we’ll have a closer look.

Side note: If you’re able to use Visual Studio, grab the Unity extensions. These will highlight the engine’s callbacks when they’re written correctly and it also suggests (and auto-completes) them if you want that.

wow, I’m blind. at least I got a response in the console now, but it doesn’t turn the bools to false.

I get this everytime there’s a wall next to the player:

NullReferenceException: Object reference not set to an instance of an object
CheckWallRight.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/CheckWallRight.cs:24)

What kind of response? An error or just some logging to see whether the method is called?

I figured it out, I had to switch out getComponent with FindObjectOfType. Thank you so much for your help :slight_smile: