Detect collision from different objects?

I couldn’t come up with a suitable title for this question but I’ll explain.

I have a collision box, which holds a script. This script has an if statement that detects collision from object “Cube001” and sends a Debug.Log to console.

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

public class cubeDetect : MonoBehaviour {

    void OnCollisionEnter(Collision collision) {

        if (collision.gameObject.name == "Cube001")
        {
            Debug.Log("Cube001 hit!");
        }
    }

}

With this method, the box collider knows what cube has touched it, as I have instructed so with

collision.gameObject.name == "Cube001"

But say if I have 10 cubes colliding with the collision box, how can I change the if statement so instead of writing another 9 if statements that check if it touches the collision box, I can just have 1 if statement that just first detects a collision from another cube, knows what cube hit the box, and with this knowledge, is able to do a Debug.Log to display the name of the cube that hit the box.

I’ve tried going through the documentation for OnCollisionEnter but couldn’t find anything to help with this.

Using layers

  1. Create a new layer called “Cubes”
  2. Assign the cubes (Cube001 → Cube010) this new layer
  3. Create a 2nd layer called “CubesDetector”
  4. Change the collision detection matrix so that the Cubes layer only detect collisions with CubesDetector
  5. In your OnCollisionEnter function, you know that the object referenced by collision.gameObject is a cube, and nothing else

Using tags

  1. Create a new tag called “Cube”

  2. Check the tag of the object in OnCollisionEnter

      if (collision.gameObject.CompareTag("Cube") )
     {
         Debug.Log(collision.gameObject.name + " hit!");
     }
    

Using regular expression

      if (System.Text.RegularExpressions.Regex.IsMatch(collision.gameObject.name, "^Cube\\d{3}$")  )
     {
         Debug.Log(collision.gameObject.name + " hit!");
     }