Is there any way to trigger an event if a variable from another scripts changes without calling it in update(). I would love to avoid calling everything in update.
In my case I have a time/date variable and I was wondering if there is a way for other scripts to see if the time was changed without using Update().
Ok course. This is one way you could do it, I wouldn’t say its the best way as I’m no expert
The script containing the variable you want to monitor;
using UnityEngine;
public class EventSender : MonoBehaviour
{
private float _myFloat = 0f;
public event OnVariableChangeDelegate OnVariableChange;
public delegate void OnVariableChangeDelegate(float newVal);
public float MyFloat
{
get
{
return _myFloat;
}
set
{
if (_myFloat == value) return;
_myFloat = value;
if (OnVariableChange != null)
OnVariableChange(_myFloat);
}
}
}
The script containing the code you wish to listen for changes in your variable;