Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

variable listener

Discussion in 'Scripting' started by Hapciupalit, Apr 29, 2017.

  1. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    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().

    Thank you,
    Hapciupalit
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You can use a get set and run a function each time the value is set or have it fire a custom event.
     
  3. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    Ok... and can put other scripts to listen to that event?
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Ok course. This is one way you could do it, I wouldn't say its the best way as I'm no expert :p

    The script containing the variable you want to monitor;

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EventSender : MonoBehaviour
    4. {
    5.     private float _myFloat = 0f;
    6.  
    7.     public event OnVariableChangeDelegate OnVariableChange;
    8.     public delegate void OnVariableChangeDelegate(float newVal);
    9.  
    10.     public float MyFloat
    11.     {
    12.         get
    13.         {
    14.             return _myFloat;
    15.         }
    16.         set
    17.         {
    18.             if (_myFloat == value) return;
    19.             _myFloat = value;
    20.             if (OnVariableChange != null)
    21.                 OnVariableChange(_myFloat);
    22.         }
    23.     }
    24. }
    25.  
    The script containing the code you wish to listen for changes in your variable;

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EventListener : MonoBehaviour
    4. {
    5.     private EventSender _eventSender;
    6.  
    7.     void Start()
    8.     {
    9.  
    10.         _eventSender = GameObject.FindObjectOfType<EventSender>();
    11.  
    12.         _eventSender.OnVariableChange += VariableChangeHandler;
    13.     }
    14.  
    15.     private void VariableChangeHandler(float newVal)
    16.     {
    17.         Debug.Log("Event Fired. MyFloat = " + newVal);
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         //For Testing
    23.         if (Input.GetKeyDown(KeyCode.DownArrow))
    24.         {
    25.             _eventSender.MyFloat -= 0.5f;
    26.         }
    27.         else if (Input.GetKeyDown(KeyCode.UpArrow))
    28.         {
    29.             _eventSender.MyFloat += 0.5f;
    30.         }
    31.     }
    32. }
     
  5. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    Thank you very much. This is just perfect. For me as long as it works ... I don't care if it's the best solution.
     
    Clayton-du-Preez likes this.
  6. SirhotBay

    SirhotBay

    Joined:
    Feb 3, 2021
    Posts:
    12
    This looks pretty useful but I cant get worked that. Even when I use exact same code I do receive nothing, even no feedback and no error.

    its an old post but if you guys are still alive, can you please help me.

    thanks