- Home /
IL2CPP Compiled Application's scripts not working
I wanted to use IL2CPP in my application to make it more performant and also harder to decompile, but whenever I compiled my game using IL2CPP, some scripts are not functining properly (Mainly ones involved in raycasting and copying components from one gameobject to another). I believe it has something to do with AOT compilation, the LOW Managed Code Stripping and or that some of my script's components are obtained via the scripts. If you have a better understanding of how IL2CPP works, please let me know what could have possibly caused this to happen.
It is really difficult to tell in general. I'd recommend checking the player log for errors or debugging the code to understand what might be wrong. See https://docs.unity3d.com/$$anonymous$$anual/$$anonymous$$anagedCodeDebugging.html for details.
@JoshPeterson I believe the issue is just the code stripper, I tried external debugging and got an exception "$$anonymous$$icrosoft C++ exception: Il2CppExceptionWrapper at memory location ..." whenever my scripts weren't functioning. This exception is probably because the part of the scripts that didn't get removed are trying to access the removed objects which is leading to a null reference exception. I tried using a link.xml file to try and solve this, yet it still says assemblies are being stripped. I also tried using [Preserve] above my classes and [assembly: Preserve] in one of my scripts, yet this issue still seems to be occuring.
It sounds like you attached the debugger to native code. I'd recommend following the directions in the link I mentioned to debug managed code. Then you might be able to discover what is being stripped unexpectedly.
Answer by unity_0sdsWZad6Fwp-A · Jun 23, 2020 at 04:11 AM
I figured out that the error had been occurring because of the method that I was using for copying a TrailRenderer to another gameobject.
public static TrailRenderer CopyTrailRenderer(this GameObject obj, TrailRenderer duplicate)
{
TrailRenderer target = obj.AddComponent<TrailRenderer>();
foreach (PropertyInfo x in typeof(TrailRenderer).GetProperties())
if (x.CanWrite)
x.SetValue(target, x.GetValue(duplicate));
return target;
}
The error was occurring on the x.SetValue line and the error message was "Get Method not found for 'some TrailRenderer property'". I fixed this by manually getting and setting the properties for the TrailRenderer.