Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by jacksmash2012 · Feb 18, 2011 at 03:18 PM · property

Is it possible to detect a property change at runtime?

Meaning... if someone changes the color property of a script in the Inspector at runtime, is there a way to detect that event?

Comment
Add comment · Show 2
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 Cherno · Jun 28, 2016 at 03:15 PM 0
Share

Could you maybe use the Set method of the Property to call some "Alert, Value has changed!" function? That way, whenever the value is changed (actually, even if it set to the same value), the relevant script will know it.

avatar image Xepherys · Jun 28, 2016 at 03:24 PM 0
Share

@Cherno - that might be a good idea. But I'd rather know what the existing method was before I went about overloading it to add a message or something. I've not mucked about with overloading internal Unity classes. Any tips on where I might start?

3 Replies

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

Answer by poncho · Feb 18, 2011 at 03:37 PM

you can "detect" it by saving the value constantly and compare it with its actual value like

color oldColor;
color newColor;
update()
{
newColor = object.color;
if(oldColor!= newColor)
{
Debug.log("The color change was realized");
}
oldColor = object.color;
}

thats one way to see the change, hope this helps you

Comment
Add comment · 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
0

Answer by Xepherys · Jun 28, 2016 at 02:21 PM

This actually isn't a valid solution due to floating point precision issues (trust me, I've been at this for a full day now). I'm working on a method that will compare a specific precision of the float for each rgba channel in two colors, but I'm trying to make it quick enough that the compare isn't detrimental when run in Update().

I've also seen a recommendation to use inverselerp, but that seems costly in Update() or FixedUpdate()

Comment
Add comment · Show 1 · 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 mgstauff · Jun 25, 2019 at 08:28 PM 0
Share

For anyone who comes across this thread - whatever solution you come up with, thought I'd share that depending on how quickly you need to detect the change, you can use a coroutine to run the check less often than every frame.

avatar image
0

Answer by voigtboy · Mar 05, 2017 at 03:26 PM

This is what I just coded up that works really well and simple to use:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
 
 public class PropertyNotifier<T>
 {
     object Instance;
     FieldInfo Field;
     MethodInfo OnChangedMethod;
 
     public PropertyNotifier(object propertyObject, string fieldName)
     {
         // verify object
         Instance = propertyObject;
 
         // pull field for the property on the object
         Field = Instance.GetType().GetField(fieldName);
         if (Field == null)
             throw new Exception(Instance.GetType().Name + "." + fieldName + " not found!");
 
         // pull changed method for the property on the object
         var changedMethodName = "On" + fieldName + "Changed";
         OnChangedMethod = Instance.GetType().GetMethod(changedMethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
         if (OnChangedMethod == null)
             throw new Exception(Instance.GetType().Name + "." + changedMethodName + " not found.  Please create it!");
     }
 
     public void Set(T value)
     {
         T currentValue = (T)Field.GetValue(Instance);
         if (currentValue == null && value == null)
             return;
         if (currentValue != null && value != null && currentValue.Equals(value))
             return;
         Field.SetValue(Instance, value);
         OnChangedMethod.Invoke(Instance, null);
     }
 
     public void ForceNotification()
     {
         OnChangedMethod.Invoke(Instance, null);
     }
 }
 
 public class TestClass
 {
     public int IntField;
     PropertyNotifier<int> IntProperty;
 
     public string StrField;
     PropertyNotifier<string> StrProperty;
 
     public TestClass()
     {
         IntProperty = new PropertyNotifier<int>(this, "IntField");
         StrProperty = new PropertyNotifier<string>(this, "StrField");
 
         IntProperty.Set(5);
         IntProperty.Set(6);
         IntProperty.Set(6);
         IntProperty.Set(7);
 
         StrProperty.Set(null);
         StrProperty.Set("hello");
         StrProperty.Set("hello");
         StrProperty.Set("world");
         StrProperty.Set(null);
     }
 
     void OnIntFieldChanged()
     {
         System.Diagnostics.Debug.WriteLine("OnIntMemberChanged to " + IntField);
     }
 
     void OnStrFieldChanged()
     {
         System.Diagnostics.Debug.WriteLine("OnStrMemberChanged to " + (string.IsNullOrEmpty(StrField) ? "nullOrEmpty" : StrField));
     }
 }
 

It produces the following output:

 OnIntMemberChanged to 5
 OnIntMemberChanged to 6
 OnIntMemberChanged to 7
 OnStrMemberChanged to hello
 OnStrMemberChanged to world
 OnStrMemberChanged to nullOrEmpty
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Change scale in lightmap (Prefab property) by script 0 Answers

How to update a property of Behaviour script? 3 Answers

how do i get property by name out of an object? 1 Answer

Is it possible to dynamically disable or validate properties in the inspector? 3 Answers


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