jeudi 27 mai 2021

Unity C# Reflection Get/Set Issue

I just can't get this working, so maybe someone can support.

I want to set the value of a specific component field programatically. Here is my code for that (Log_temp is the component):

                FieldInfo info = Log_temp.GetType().GetField("Driver_" + tour.Driver);
                TypedReference infoStructRef = __makeref(Log_temp);

                JSON Tour_Json = JSON.Serialize(tour);

                info.SetValueDirect(infoStructRef, Tour_Json.CreateString());

This works perfectly fine. Now I wanted to not only set the value, but ADD the (string) value to the existing value in the field. Here is what I tried:

            FieldInfo info = Log_temp.GetType().GetField("Driver_" + tour.Driver);
            TypedReference infoStructRef = __makeref(Log_temp);

            JSON Tour_Json = JSON.Serialize(tour);

            info.SetValueDirect(infoStructRef, info.GetValueDirect(infoStructRef).ToString() + Tour_Json.CreateString());

I tried a few other approaches but either I get an error or nothing changes. Could someone help me with this? Thanks a lot!





Aucun commentaire:

Enregistrer un commentaire