In my first script, I declare an event like so
public static event Action OnChangeScene;
private void OnMouseDown()
{
OnChangeScene?.Invoke();
}
In my second script, I try to get Destroy(gameObject) to subscribe to that event via a lamda expression.
private void Start()
{
MoveButton.OnChangeScene += () => Destroy(gameObject);
}
The problem is that it only works the first time the mouse is clicked. After that, I get the error, "MissingReferenceException: The object of type 'Animal' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."