- Home /
Why does one scipt give an error, when I have a script using #pragma strict?
I had a project that suddenly gave me errors when I started adding #pragma strict to my scripts
I removed the code not giving errors, and ended up with these two scripts:
1) PragmaScript: (yes, this is the entire script)
#pragma scrict
2) ScriptInFolder:
function Start ()
{
var names : Array = new Array();
names.Push("John");
print(0 + names[0]);
}
ScriptInFolder must be in a folder and running on a gameobject. PragmaScript can be placed anywhere.
The runtime error is:
InvalidProgramException: Invalid IL code in ScriptInFolder:Start (): IL_001f: add
Why does this happen? Shouldn't javascript "fix" these things on the fly, even though I use #pragma strict in one script?
Can you elaborate on what you did when you played around before it suddenly got fixed?
Answer by Mike 3 · Nov 03, 2010 at 06:28 PM
BCE0051: Operator '+' cannot be used with a left hand side of type 'int' and a right hand side of type 'Object'.
That's the error you'd be getting if you had #pragma strict in your second script
change it to
print("0" + names[0]);
and it should work ok
What's happening is that since you're not using #pragma strict in the second script, it's looking at the two objects and can't work out what it should do until runtime. Unfortunately it guesses wrong, and screws up when it tries to do an integer addition
Making it string + object should call .ToString() implicitly on the second object, fixing it
Just as I thought things weren't going to get any stranger. The code you posted only works if I actually put #pragma strict in ScriptInFolder. If I don't do that, then I get a "IL_001e: add" error at runtime
Heh. print("0" + names[0].ToString()); should work with both.
Yup it does :) But overall, with all the oddities involved I think I will report this as a bug. Also, would there be any way to declare the array to hold strings? (not builtin)
List. names = new List.(); you'll need import System.Collections.Generic; at the top of your script
Oh sorry - var names : List. = new List.(); my code before was half unityscript, half c# :P
Answer by Ulrich · Oct 29, 2010 at 02:14 AM
try with "function Start ()" instead
Ulrich, I know you mean well. It's just that Atnas was pointing out that what you had suggested as an answer did not really help because what you had suggested for Atnas to use was, in fact, already being used in the script.
Answer by soulburner · Feb 21, 2013 at 11:19 PM
Yeah, I've just had the same error while trying to perform the following:
string + int + int + string
the error dissappeared when I've changed it to:
string + int + "" + int + string
Your answer
Follow this Question
Related Questions
#pragma strict exhaustive list of types 1 Answer
Invalid IL code exception when calling method 2 Answers
No appropriate version of 'UnityEngine.Object.Destroy' for the argument list '(Object)' was found. 1 Answer
BCE0051 Error 1 Answer
Pragma strict : GetComponent without knowing the name of the component 3 Answers