MerlinVR / UdonSharp

An experimental compiler for compiling C# to Udon assembly
MIT License
678 stars 89 forks source link

Using UdonBehavior.publicVariables.TrySetVariableValue results in a compile error. #9

Closed PintsizedSix40 closed 4 years ago

PintsizedSix40 commented 4 years ago

Describe the bug in detail: Using UdonBehavior.publicVariables.TrySetVariableValue results in a compile error. The error output is System.Exception: Could not find Udon field accessor VRCUdonCommonInterfacesIUdonEventReceiver.__get_publicVariables__VRCUdonCommonInterfacesIUdonVariableTable.

Provide steps/code to reproduce the bug: Get a gameobject with a UdonBehavior, get the component using (UdonBehaviour)gameobject.GetComponent(typeof(UdonBehaviour)), then attempting to set the variable using .publicVariables.TrySetVariableValue("name", value).

(UdonBehaviour)gameobject.GetComponent(typeof(UdonBehaviour)).publicVariables.TrySetVariableValue("name", value);

Expected behavior: The variable is set correctly.

Additional Information:

PhaxeNor commented 4 years ago

You have to use SetProgramVariable(name, value)

MerlinVR commented 4 years ago

Thanks for the bug report! I made the error messages clearer about this in a recent commit that's going to be in a release tomorrow. This is because Udon only exposes a subset of functions that exist in Unity. If you get any errors about methods, properties, or fields not being exposed or found it means that Udon does not currently expose that function for people to call. Sometimes this is for security reasons and sometimes it's just because VRChat hasn't realized that the function should be exposed.

In this case you can have the same functionality by instead calling SetProgramVariable() on the UdonBehaviour and passing the name of the variable and the value.

I'm working on more documentation, but if you want to set a variable on another UdonSharpBehaviour, you can store it as the actual type and then set the field on it as you normally would in C#. So you can do gameobject.GetComponent<YourType>().name = value;