Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Property.SetValue() doesn't take two arguments?

Discussion in 'Scripting' started by Riderfan, Jan 16, 2019.

  1. Riderfan

    Riderfan

    Joined:
    Jan 10, 2013
    Posts:
    460
    Hello.

    I'm using Reflection to set a property value however the method overloads while in Unity don't match either the Microsoft documentation or those used in a non-Unity application.

    For example, I should be able to do this;

    Code (CSharp):
    1.  
    2. System.Reflection.PropertyInfo Prop = ObjectName.GetType().GetProperty("PropertyName");
    3.  
    4. //Apply the new value -- this uses a property look up so we don't need to iterate through every property name.
    5. Prop.SetValue(null, 2);
    6.  
    This works fine in a normal application, but in Unity this fails to compile because for some reason, Unity expects 3 parameters. I'm doing this because I can not explicitly set the property value directly because the exact one is figured out at run time -- if that makes any sense.

    Has anyone had any luck doing this sort of thing?

    thanks
     
  2. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    784
    PropertyInfo is something implemented in the runtime, in the case of Unity that would be "Mono".

    SetValue should work exactly the same in unity (mono) as it does in the normal .net framework or in .net core.

    Can you tell us what the names and types of those supposedly 3 parameters are?

    So far I'm working with reflection for many years now and I've never encountered something like this; so maybe it's an extension method? But then again, that definitely wouldn't "hide" the normal methods... curious.

    Also, press f12 to see where that method is defined at. Maybe it shows you some unexpected dll