Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Selene · Jul 26, 2011 at 01:52 PM · static-function

InvalidProgramException: Invalid IL code - when using static function

Hello,

I have a script, from where I want to use a static function:

 // RenderScript.js
 static function DoRender(c:Color){
     var childrenRenderer : Renderer[];
     childrenRenderer = GetComponentsInChildren.<Renderer>();
 
     for (var childRenderer : Renderer in childrenRenderer) {
         childRenderer.material.color = c;
     }
 }

When im trying to use it in some other script:

 function OnMouseOver() {
     RenderScript.DoRender(Color.red);
 }

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. I also searched the forums regarding this error, but couldn't find anything useful so far...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Peter G · Jul 26, 2011 at 01:58 PM

I don't know if this is causing the error, but you are calling an instance method from your static function.

 // RenderScript.js
 static function DoRender(c:Color){
     var childrenRenderer : Renderer[];
     childrenRenderer = GetComponentsInChildren.<Renderer>();
 
     for (var childRenderer : Renderer in childrenRenderer) {
         childRenderer.material.color = c;
     }
 }

GetComponentsInChildren.<Renderer>() is an instance method. You can't call it without having an object and since you are using a static method, there is no instance. You will need a different way of gathering all the renderers, maybe making a singleton and then accessing the renderers through that.

How to implement a singleton in js. You can do it with properties if you want to but it isn't really necessary.

 //Singleton.js
 private static var rendererParent : GameObject;
 
 public static function GetRendererParent () : GameObject {
       if(rendererParent == null) {
            rendererParent = GameObject.Find("Some GameObject");
       }
       return rendererParent;
 }

then find the instance using GetRendererParent() and call GetComponentsInChildren() with that. Note that this will always use the same instance, thats why its a singleton. If you want to be able to change the game object, then you will need to do something different such as pass the parent into the function.

Edit:

If the parent object is varying, you probably don't want it a singleton. Your code was close, but you want to pass the reference to the game object into the function.

 //Not a Singleton
 public static function DoRender (_objectName : String , c : Color) {
 
     var  rendererParent = GameObject.Find(_objectName);
     //Find a GameObject with the name you pass into the function.
 
     //gather all the renderers in its children.
     var childrenRenderer : Renderer[] = rendererParent.GetComponentsInChildren.<Renderer>();
 
     for (var childRenderer : Renderer in childrenRenderer) {
 
        childRenderer.material.color = c;
 
     }
 }

or an overloaded method that takes the game object directly. The compiler can infer which method to use based on what arguments you pass.

 //overloaded method.  Takes a GameObject and a color rather than a string and a color.
 public static function DoRender (_object : GameObject , c : Color) {
 
     //gather all the renderers in its children.
     var childrenRenderer : Renderer[] = _object.GetComponentsInChildren.<Renderer>();
   
     for (var childRenderer : Renderer in childrenRenderer) {
 
        childRenderer.material.color = c;
 
     }
 }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Selene · Jul 26, 2011 at 02:21 PM 0
Share

Thanks for the quick answer. Probably you are right and that's why I am getting the error. Now my problem is that I didn't used singletons before, so I'm not sure how to start with that. Would you have any tips on how could I change my script?

avatar image Peter G · Jul 26, 2011 at 03:31 PM 0
Share

Edited my answer

avatar image Graham-Dunnett ♦♦ · Jul 26, 2011 at 06:07 PM 0
Share

awesome answer!

avatar image Selene · Jul 27, 2011 at 08:12 AM 0
Share

Thanks for the answer. I modified it a little and now it works just as I wanted. :) This really helped me out.

avatar image Selene · Jul 27, 2011 at 04:09 PM 0
Share

Hi again, It seems that I still have a problem with the script, below you can find what I modified. It seems that it still only works for one object, but for more than one object it's not ok. Can you help me out with this?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how can i fix this Really weird error 1 Answer

static function generates error CS0119: 1 Answer

Why does one scipt give an error, when I have a script using #pragma strict? 3 Answers

Static vs Non-Static Methods 1 Answer

Invalid IL code exception when calling method 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges