Storing the gameObjects of colliders detected in OnCollisionEnter

I am in the very early stages of trying to replicate the physics in the game portal, specifically the snapping feature that portals have when they are put in a place where they would not fit (a corner) and instead place to the nearest possible position. The way I test to see if it will be too close is by first placing a box collider where the portal would go, and detect if it collides with any walls. I am trying to store these walls in a List in order to use their positions to offset the desired location for the portal, but every time I test I get the following NullReferenceException:

NullReferenceException: Object reference not set to an instance of an object
CollisionInfo. OnTriggerEnter (UnityEngine.Collider other) (at Assets/CollisionInfo.cs:12)

Here is the very short script attached to the box collider I use to check for walls. It seems every time I test, although it definitely collides with walls, the List is always empty.

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

public class CollisionInfo : MonoBehaviour
{
    public static List<GameObject> walls;

    void OnTriggerEnter(Collider other)
    {
        walls.Add(other.gameObject);
    }
}

Thanks in advance for any help. If you need the other class for actually placing the portals let me know

Find out what is null.
Debug.Log(other);
Debug.Log(other.gameObject);
Debug.Log(walls);

On of those will fail, that’s null.
My guess would be, but I’m not sure about that, that you will have to Initialize the List somewhere as it is not only public, but also static. Only Public would be initialized by Unity.

1 Like

Even when I log the colliders, nothing shows up in console. It seems the box collider does not count teleporting into another object as entering collision, and therefore does not add it to the list. When I walk into the hitbox however, it does log my player hitbox. Is there any other way to log these collisions?

Edit: tried this bit of code with OnTriggerStay and it still does not log anything. (Each wall is tagged “Obstacle”)

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

public class CollisionInfo : MonoBehaviour
{
    public static List<GameObject> walls;

    private bool newObstacle;

    void Start()
    {
        walls = new List<GameObject>();
    }

    void OnTriggerStay(Collider other)
    {
        newObstacle = true;
        if(other.transform.tag == "Obstacle")
        {
            for(int i = 0; i < walls.Count; i++)
            {
                if(walls[i] == other.gameObject)
                {
                    newObstacle = false;
                    break;
                }
            }
            if(newObstacle)
            {
                Debug.Log(other);
                Debug.Log(other.gameObject);
                Debug.Log(walls);
                walls.Add(other.gameObject);
            }
        }
    }
}

if you have

and nothing shows up then your collision is not working. Because of Layers that don’t check collision with each other? Because of the collision matrix when it comes to triggers? See here: Unity - Manual: Introduction to collision

Does the object holding the BoxCollider or the walls have a Rigidbody ?
Collision between two object who don’t have a rigidbody does not trigger any collision event.