How to reference a collision event from one script in another?

I am creating a program, called Reflective_Door, that, upon detecting that a character has entered a room, called “Reflection”, will close a door behind them.

So far, I have created a simple collision detector in the room, called “Reflection_Trigger”.

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

public class Reflection_Trigger : MonoBehaviour
{
    void OnCollisionEnter(Collision collision)
    {
       
    }
}

I have also started the creation of the script that will detect whether my character has entered Reflection, and close Reflective_Door.

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

public class Reflective_Door : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameObject.Find("Reflection").GetComponent<Reflection_Trigger>().collision;
    }
    // Update is called once per frame
    void Update()
    {

        if (collision == true)
        {
            if (transform.position.x == 0.0f)
            {    
                    transform.position = new Vector3(-9.06f, 2.56f, 74.87f);    
            }
        }
    }    
}

Upon attempting to run the program, I am met with the following errors:

  • Assets/Reflective_Door.cs(12,74): error CS1061: ‘Reflection_Trigger’ does not contain a definition for ‘collision’ and no accessible extension method ‘collision’ accepting a first argument of type ‘Reflection_Trigger’ could be found (are you missing a using directive or an assembly reference?)

  • Assets/Reflective_Door.cs(12,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

  • Assets/Reflective_Door.cs(18,13): error CS0103: The name ‘collision’ does not exist in the current context

With this information in mind, what should I create to allow for Reflective_Door to properly detect the collision in Reflection_Trigger and act upon it?

I am new to Unity, so any help is appreciated. Thank you!

collision is not defined in your Reflective_Door script, it’s defined as a method parameter in OnCollisionEnter of Reflection_Trigger and as such it only exists in the OnCollisionEnter method, it can not be accessed or used anywhere else in that script or any other, this is called variable scope.

To do it the way you are trying you would have to define a bool for collision in your Reflective_Door script and set it’s value in your Reflection_Trigger script by creating a reference to your Reflective_Door script then accessing the script through that reference, you will need to make your collision bool public so it can be accessed in other scripts, this is called access modifiers, or have a public method to set the variable.

However this is all really inefficiently setup, you are constantly checking in update whether this bool has been triggered which may only happen once in your game, this is called polling, what you should do is make your code more reactive, meaning you should not use update for this, what you do is everything you want to happen when they player goes in the room, you put in your own method, then you call that method from the collision of the trigger script and you don’t need to declare the bool.

 public class Reflection_Trigger : MonoBehaviour
 {
     public Reflective_Door reflectiveDoor;
// this is the reference to your script you use your script name as the variable type and make it public so you can assign it in the inspector

     void OnCollisionEnter(Collision collision)
     {
          relectiveDoor.CloseDoor();
//this uses the reference to call the method defined in the other script
     }
 }

public class Reflective_Door : MonoBehaviour
{
    public void CloseDoor()
    {
         transform.position = new Vector3(-9.06f, 2.56f, 74.87f);
    }    
 }

Basically you can access Reflection_Trigger from Reflective_Door, but it is easier and more efficient do do it the other way.