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

InvalidProgramException: Invalid IL code - when using static function

Discussion in 'Scripting' started by Selene, Jul 26, 2011.

  1. Selene

    Selene

    Joined:
    Mar 2, 2011
    Posts:
    25
    Hello,

    I have a script, from where I want to use a static function:
    Code (csharp):
    1.  
    2. // RenderScript.js
    3. static function DoRender(c:Color){
    4.     var childrenRenderer : Renderer[];
    5.     childrenRenderer = GetComponentsInChildren.<Renderer>();
    6.  
    7.     for (var childRenderer : Renderer in childrenRenderer) {
    8.         childRenderer.material.color = c;
    9.     }
    10. }
    When im trying to use it:
    Code (csharp):
    1. function OnMouseOver() {
    2.     RenderScript.DoRender(Color.red);
    3. }
    I get the following error: "InvalidProgramException: Invalid IL code in RenderScript: DoRender (UnityEngine.Color): IL_0003: callvirt 0x2b000001".

    Anyone any idea what could be wrong?
    Thank you.

    PS. When I'm using the DoRender() function from the same script, where the OnMouseOver() is, it works without any problems.
     
    Last edited: Jul 26, 2011
  2. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    301
    I got this same error when using the fastJSON library. Not really related, I know, but I'd still like some light shed on this situation :p

    My variant:

    InvalidProgramException: Invalid IL code in (wrapper dynamic-method) object:_ (object,object): IL_000c: callvirt 0x00000005

    Thrown by the last line (return) of this code block:

    Code (csharp):
    1.  
    2.         private delegate void GenericSetter(object target, object value);
    3.  
    4.         private static GenericSetter CreateSetMethod(PropertyInfo propertyInfo)
    5.         {
    6.             MethodInfo setMethod = propertyInfo.GetSetMethod();
    7.             if (setMethod == null)
    8.                 return null;
    9.  
    10.             Type[] arguments = new Type[2];
    11.             arguments[0] = arguments[1] = typeof(object);
    12.  
    13.             DynamicMethod setter = new DynamicMethod("_", typeof(void), arguments);
    14.             ILGenerator il = setter.GetILGenerator();
    15.             il.Emit(OpCodes.Ldarg_0);
    16.             il.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
    17.             il.Emit(OpCodes.Ldarg_1);
    18.  
    19.             if (propertyInfo.PropertyType.IsClass)
    20.                 il.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
    21.             else
    22.                 il.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
    23.  
    24.             il.EmitCall(OpCodes.Callvirt, setMethod, null);
    25.             il.Emit(OpCodes.Ret);
    26.  
    27.             return (GenericSetter)setter.CreateDelegate(typeof(GenericSetter));
    28.         }
    29.  
    I thought it was pretty straightforward, so I don't know why I'm getting this error.