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 Ian9921 · May 19, 2016 at 04:56 AM · javascripterrorstaticcall

Invalid IL code error when calling static function

I'm making a game where the player can fly via

 static var jumpHeight = 8;
 
 static function fly (){
     GetComponent.<Rigidbody>().velocity.y = jumpHeight;
 }

which is being called by

 if (Input.GetKey (KeyCode.Space))
     {
         if (MainScript.flying == true)
         {
             FlightScript.fly ();
         }    
     }

However, when I press space, I get an error reading:

InvalidProgramException: Invalid IL code in FlightScript:fly (): IL_0008: ldarg.0

I have absolutely no idea what this error means or what is causing it. All I know is that whatever is causing it is keeping my game from working. If anyone has any experience with this error please tell me how to fix it.

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
2

Answer by corn · May 19, 2016 at 10:45 AM

The problem is right there :

  static function fly (){
      GetComponent.<Rigidbody>().velocity.y = jumpHeight;
  }

This cannot work. fly is a static function, which means it is not associated to any instance of your script, therefore it is not associated to any GameObject in your scene. GetComponent is not static, it needs a GameObject instance.

You need to actually retrieve a reference to the player character before you can get its rigidbody, or use a static instance and make your script a Singleton.

See this detailed answer if you need more information.

Comment
Add comment · Show 6 · 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 Ian9921 · May 20, 2016 at 01:55 AM 0
Share

@corn I have never used a Singleton before. How would I use one in this context?

avatar image Ian9921 · May 22, 2016 at 10:24 PM 0
Share

@corn All right, I've almost got it to work. $$anonymous$$y new code looks like this.

 #pragma strict
 
 static var jumpHeight = 100;
 
 private static var flightParent : GameObject;
 
 public static function fly () {
 
 var childrenFlight : Rigidbody[];
 
 
     if(flightParent == null) {
 
         flightParent = GameObject.Find("FPSController");
 
     }
 
     childrenFlight = flightParent.GetComponentsInChildren.<Rigidbody>();
 
     for (var childFlight : Rigidbody in childrenFlight) {
 
         GetComponent.<Rigidbody>().velocity.y = jumpHeight;
 
     }
 }

However, Now I get a similar error saying: Invalid IL code in FlightScript:fly (): IL_0046: ldarg.0

Could you be a little more specific this time?

avatar image Bunny83 Ian9921 · May 22, 2016 at 10:34 PM 0
Share

You don't use "childFlight" inside your for loop. You probably want to do something like this:

 for (var childFlight : Rigidbody in childrenFlight) {
     childFlight.velocity.y = jumpHeight;
 }

Your GetComponent call will still cause the same error since GetComponent is an instance method which needs an explicit object reference. If no object reference is given "this" is assumed by the compiler. However "this" is not valid inside a static method since a static method is not bound to a specific object.

avatar image Ian9921 Bunny83 · May 22, 2016 at 11:41 PM 0
Share

@Bunny83 @corn Alright, now my code throws no errors, but does absolutely Nothing and looks like this:

 #pragma strict
 
 static var jumpHeight = 1000000;
 
 private static var flightParent : GameObject;
 
 public static function fly () {
 
 var flightRigidbody : Rigidbody[];
 
 
     if(flightParent == null) {
 
         flightParent = GameObject.Find("FPSController");
     }
 
     flightRigidbody = flightParent.GetComponents.<Rigidbody>();
 
     for (var parentFlight : Rigidbody in flightRigidbody) {
 
         parentFlight.velocity.y = jumpHeight;
 
     }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

"Failed to call static function because an object was provided" error? 1 Answer

An instance of type 'TurretControl' is required to access non static member 'Shoot'. 1 Answer

Cannot use Static var/ Unknown Idientifier 1 Answer

Confusing Error 1 Answer

Script is causing immense lag, and I don't know what's causing it. 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