This is a follow-up to a previous post I made regarding one script triggering another script. I have been able to trigger a collision, but upon the collision being triggered, the function in the second script isn’t being called, and instead I receive this error:
NullReferenceException: Object reference not set to an instance of an object
Reflection_Trigger.OnTriggerEnter (UnityEngine.Collider collision) (at Assets/Reflection_Trigger.cs:18)
Here is the first script, entitled Reflection_Trigger, which triggers the second script upon detection of a collision:
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class Reflection_Trigger : MonoBehaviour
{
public Reflective_Door reflectiveDoor;
void OnTriggerEnter(Collider collision)
{
if (collision.transform.gameObject.name == "PlayerCapsule")
{
Debug.Log("Collided!");
reflectiveDoor.CloseDoor();
Debug.Log("Function Called!");
}
}
}
Here is the second script, entitled Reflective_Door, which should be called by the first script upon collision detection, and move an assigned object to another location:
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class Reflective_Door : MonoBehaviour
{
public void CloseDoor()
{
Debug.Log("Door Closed!");
transform.position = new Vector3(-9.06f, 2.56f, 74.87f);
}
}
I want to know how I can resolve the NullReferenceException error and successfully trigger one script using another. I am new to Unity so any help on this would be greatly appreciated. Thank you.