- Home /
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.
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.
@corn I have never used a Singleton before. How would I use one in this context?
@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?
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.
@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;
}
}
Your answer
Follow this Question
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