Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug IL2CPP - Reflection - Error in build

Discussion in 'Editor & General Support' started by Doomchecker, Dec 22, 2022.

  1. Doomchecker

    Doomchecker

    Joined:
    Apr 5, 2021
    Posts:
    105
    Tested with Unity 2021 LTS and 2022.2.

    In a new project I attach this script to a Cube.

    Code (CSharp):
    1. using System.Reflection;
    2. using UnityEngine;
    3.  
    4. public class ReflectionTest : MonoBehaviour
    5. {
    6.     public Component transformComponent;
    7.     public Vector3 newValue = new(50, 50, 50);
    8.     private PropertyInfo propertyInfo;
    9.  
    10.     private void Start()
    11.     {
    12.         propertyInfo = transformComponent.GetType().GetProperty("localEulerAngles");
    13.         propertyInfo.SetValue(transformComponent, newValue);
    14.     }
    15. }
    So when I hit play, the Cube rotates.
    Building the game with Mono it also works, but building with IL2CPP gives problems.
    It builds, but when starting the game I get this error:

    Code (CSharp):
    1. ArgumentException: Set Method not found for 'localEulerAngles'
    2.   at System.Reflection.RuntimePropertyInfo.SetValue (System.Object obj, System.Object value, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00000] in <00000000000000000000000000000000>:0
    3.   at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in <00000000000000000000000000000000>:0
    4.   at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value) [0x00000] in <00000000000000000000000000000000>:0
    5.   at ReflectionTest.Start () [0x00033] in C:\UnityProjects\IL2CPP TEST\Assets\ReflectionTest.cs:16
    Should I report a bug here or maybe I'm missing something?
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    This is not a bug, but rather expected behavior. IL2CPP uses managed code stripping by default to remove unused code, which makes the final executable for your game smaller. Mono can also use managed code stripping, but it is disabled by default.

    The static analysis done by the managed code stripping system does not understand that the localEulerAngles property is used a run time, so it is removed. You can tell the managed stripping system about this property by using a link.xml file.

    You can find many more details in the documentation here: https://docs.unity3d.com/Manual/ManagedCodeStripping.html
     
  3. Doomchecker

    Doomchecker

    Joined:
    Apr 5, 2021
    Posts:
    105
    I tried to use the [Preserve] (and [assembly: Preserve]) attribute and also tried multiple variations using a link.xml with no success.

    If you could maybe give a clue what the link file would look like in the above example?
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Note that [Preserve] (and [assembly: Preserve]) will only work for code that you compile. The localEulerAngles property is part of a Unity Engine assembly, so you cannot apply a Preserve attribute to it.

    You can use the following link.xml file:

    <linker>
    <assembly fullname="UnityEngine.CoreModule" preserve="all">
    </assembly>
    </linker>

    This will keep everything in UnityEngine.CoreModule.dll. You can also keep specific properties if you only need a few of them.
     
    Doomchecker likes this.