Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
4
Question by Zwusel · Jun 22, 2016 at 05:15 PM · variablesvalueevent triggering

Trigger event on variable change

Hi there, how to make a custom event that triggers when a variables value is changed.

thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by jdean300 · Jun 22, 2016 at 05:31 PM

Using simple variables, the best you can do is constantly check in update whether or not is has changed:

 private int m_MyVar = 0;
 public int myVar = 0;
 
 public delegate void OnVariableChangeDelegate(int newVal);
 public event OnVariableChangeDelegate OnVariableChange;
 
 private void Update()
 {
     if (myVar != m_MyVar && OnVariableChange != null)
     {
         m_MyVar = myVar;
         OnVariableChange(myVar);
      }
 }

However, if you can use properties this is a little cleaner because you know when a variable is set:

 private int m_MyVar = 0;
 public int MyVar
 {
     get {return m_MyVar;}
     set {
         if (m_MyVar == value) return;
         m_MyVar = value;
         if (OnVariableChange != null)
             OnVariableChange(m_MyVar);
     }
 }
 public delegate void OnVariableChangeDelegate(int newVal);
 public event OnVariableChangeDelegate OnVariableChange;

To subscribe and listen to either of these events:

 private void Start()
 {
     componentWithEvent.OnVariableChange += VariableChangeHandler;
 }
 
 private void VariableChangeHandler(int newVal)
 {
     //do whatever
 }

Comment
Add comment · Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zwusel · Jun 22, 2016 at 05:45 PM 0
Share

Thanks for the fast answer. Constantly checking for my variables is what i am doing at the moment but i think doing it with events will be more efficient ;) So i will try your second option...seems that is exactly what i need.

ty very much

avatar image Zwusel · Jun 22, 2016 at 06:43 PM 0
Share

Hm i tried it but i wont get it to work:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class DamageUI : $$anonymous$$onoBehaviour
 {
     public static float damage;
     public static float $$anonymous$$Damage;
     public static float maxDamage;
 
     [SerializeField]
     private Slider slider;
 
     Text damageText;                      // Reference to the Text component.
 
 
     void Awake()
     {
         // Set up the reference.
         damageText = GetComponent<Text>();
     }
 
 
     void Start()
     {
         slider.$$anonymous$$Value = $$anonymous$$Damage;
         slider.maxValue = maxDamage;
         slider.value = maxDamage / 2;
     }
 
 
     void Update()
     {
         damage = slider.value;
         damage = $$anonymous$$athf.Round(damage);
 
         // Set the displayed text to be the word "Score" followed by the score value.
         damageText.text = "Damage: " + damage;
     }
 
 
     public float Damage
     {
         get
         {
             return damage;
         }
 
         set
         {
             if (damage == value)
                 return;
 
             damage = value;
 
             if (OnChangeDamage != null)
                 OnChangeDamage(damage);
         }
     }
 
     public delegate void ChangeDamage(float _damage);
     public static event ChangeDamage OnChangeDamage;
 }
 

When i try to get a Debug.Log on it from another class:

 void Start()
     {
         ChangeDamageEvent.OnChangeDamage += DamageChanged;
     }
 
 
 void DamageChanged(float _damage)
     {
         Debug.Log("Damage changed to: " + _damage);
     }


Nothing shows up on the console when i drag the slider around so the damage value changes

avatar image jdean300 Zwusel · Jun 22, 2016 at 06:46 PM 0
Share

Did you ever subscribe to the event? you need something like: OnChangeDamage += objectThatRespondsToEvent.FunctionThatHandlesEvent;

avatar image jdean300 Zwusel · Jun 22, 2016 at 06:52 PM 0
Share

Okay I think I see what you are trying to do:

 void Start()
 {
     //Add the following to the end of your start
     OnChangeDamage += UpdateDamageText;
 }
 
 private void UpdateDamageText(float dmg)
 {
     damageText.text = "Damage: " + dmg;
 }

although you could just handle this inside the property setter:

 public float Damage
      {
          get
          {
              return damage;
          }
  
          set
          {
              if (damage == value)
                  return;
  
              damage = value;
  
              damageText.text = "Damage: " + damage;
          }
      }

Events are better suited for communicating changes in one script to another script.

avatar image Zwusel · Jun 22, 2016 at 07:18 PM 0
Share

Isnt that exactly what i do here?

 void Start()
      {
          ChangeDamageEvent.OnChangeDamage += DamageChanged;
      }
  
  
  void DamageChanged(float _damage)
      {
          Debug.Log("Damage changed to: " + _damage);
      }
 
 

Handling it inside the property setter is not an option... i need to do that in another class.

There are more locations that need to know the dmg has changed. Thats what i need the event for ;)

avatar image Zwusel · Jun 22, 2016 at 07:22 PM 0
Share

wait a $$anonymous$$ute...i might have the solution...

avatar image Zwusel · Jun 22, 2016 at 07:24 PM 0
Share

darn no...i thought i found my mistake...but no

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

48 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing a value over time 1 Answer

How do I NOT play a sound on the first selected button. 2 Answers

Change variable value while running. 1 Answer

[C#] Instantiating via class references (Need help understanding) 1 Answer

What do I do when This error shows up ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges