Bug found. Submitted. Thought I'd let you know

Hey guys n gals.

I just found a rather curious problem with Unity 3.4. As it is, I am not a big fan of MonoDevelop. Unitron just feels so much more… right! I can’t explain it. Anyways, I use Unittron for everything and just switch to MonoD if I run into a bug I can’t track down.

So yeah, here I am, coding away in Unitron, my code compiles cleanly and yet at runtime my code throws an “Object reference not set to an instance of an object” error. Granted, it is in Update so perhaps this class of mine has not yet been instantiated… even though I do call it in Start… So I change my coding. Instead of referencing the variable, I now say: If (myvar) Debug.Log(myvar.curVal = “”); … and the Debig.Log still throws out the same error!!! What the hell??? How can Debug.Log() printing a normal int cause this error??? And then I noticed it…

This code of mine actually compiled cleanly! Look carefully at the variable in use. I make reference to a variable that doesn’t exist, but the compiler doesn’t issue any errors until runtime… Perhaps because the var I use is spelled the same as the one that DOES exist up to where the name ends??? I dunno. But for some reason using curVal compiles clean even though the variable is called curValue…

Go figure…

//myfile.js
class myClass extends Object
{
   var curValue    : int = 50;
}

var myVar : myClass;

function Start()
{
   myVar = new myClass();
}

function Update()
{
Debug.Log(myVar.curVal);  <----- LOOK AT THIS!!!
}

Edit: Just tried something else.

class crCountDownTimer extends Object
{
	var val			: int;
}

class crTimedStat extends Object
{
	var curValue				: int;
	var timer					: crCountDownTimer;
}

var FatigueTimer				: crTimedStat;

function FatigueUpdate(amount)
{
	if (FatigueTimer != null)
	{
		Debug.Log(FatigueTimer.curVal + "<----");
		FatigueTimer.timer.value = 60;  <-- THIS COMPILES CLEANLY
	}
}

Use “#pragma strict”, then try that again.

–Eric

@Eric
Even since I started using Unity 4 years ago, I have never had to use that (or in fact ANY) #pragmas in any of my JS files. They all just worked perfectly.

Then they include this:

They say that #pragma strict is now a lot more strict than before, resulting in code that DID compile cleanly now no longer compiling…

I am experiencing the opposite. If I recall correctly, Unity at one point made #strict the default, thereby removing the need to explicitly declare it… but I am speaking under correction here… anyways…

NOW I am able to call functions that don’t even EXIST and THAT code still compiles cleanly…

Surely that is not correct? I upgraded my Unity from 3.3 to 3.4 and because of that I now have to add the #pragma to all 40 scripts before I can be sure that Unity’s debugger will pick up my spelling mistakes and NOT compile cleanly when I call functions the don’t exist? That doesn’t sound right to me.

I have had this class that I have been using for years. I recently changed that class a tad:

//before I had to do this:
gk = myVar.GetCurrentKey();
gk.getLabel();

//then I added this function to the base class
function getLabel()
{
   curKey.getLabel();
}

//so now I just go
myVar.getLabel();

…as a result, the GetCurrentKeyfunction became obsolete and I deleted it from the class. Now, normally I would rely on Unity to tell me which files reference a function that doesn’t exist so I can go change that code. Now, it doesn’t. Files that still call GetCurrentKey() still compile perfectly until runtime when it packs up…

Clearly the compiler is not functioning correctly if it allows you to call functions that don’t exist? So if this can be solved simply by adding the pragma to every single file from now on, then I must ask the question: “Why?”

I mean,let’s be honest here, what is the point of a compiler that allows this:

var myvar = 10;

function myfunc()
{
Debug.Log("My variable " + myvariable);
}

function Start()
{
   SomeRandomName();
}

I am one of those people who like my dear old Unitron. I am not a Monodevelop fan due to it’s crapy search function and I can’t seem to configure it so it doesn’t jerk when I scroll so it just annoys me. So I just stick to Monodevelop that works and I’m happy.

Now, interestingly enough, whenever I work in c# and I pick up an error, double clicking the error in Unity takes me to a completely wrong line in the source. 0.o For instance, yesterday I got a warning that “Object is ambigious and redefines System.Object” or something like that and when I double click it, Unitron is opened and the cursor is placed on a line of white space… Yup. Nothing on that line except for 2 tabs and the return character. The actual fault was 15 lines further down… So I did a test and I found the problem…

Whenever Unity reports the line number that contains an error in c# code, it ignore lines that are comments… If you have a script where you comment every other line of code and write descriptions for each function, if Unity tells you the problem is on line 43, well then, you will have to count off 43 lines of code from the top of the script cause the error will actually occur on line 237 if you add in all the comment lines. You can’t even go and count the comment lines and just add it to the error line Unity reports cause it only skips the comments BEFORE the error so how will you know when to stop error lines…? It is much quicker to just go to the top of the page and start counting 43 lines that aren’t just comments…

…alternatively, don’t comment your c# code.

I haven’t actually checked if this is the case in mono develop, obviously, but I should assume it is since the error message inside Unity lists the wrong line number. Has nobody else ever picked up on this?

Are you saying Unity is reporting the line number incorrectly or your editor doesn’t jump to the correct line? I’ve never used Unitron but I’ve used both Visual Studio and MonoDevelop and that has never once been an issue for me.

Unity is reporting the line number wrong and then jumping to the “correct” line according to where it believes the problem is…
Example, take a script that works… now go to any line of code and remove the “;”. Save the file.

Unity will now report an error in line X and jump to line X. Now go to the top of the script and add this:

//this
//is
//a
//test

and hit save. Unity will now still report the error on the same line of code and if you double click on the error, it will take you to 4 lines above where the error was last time…

Check out this little test Idid. I took out a “;” and it reported an error on line 12. I then hit enter on line 11 and the error shifted to say there is a mistake on line 13. Fine. So far it works. Then I went to line 12 and turned that empty line into a comment and voila… Now the error is no longer on line 13 but back on 12 again meaning the error is now moved one line up. But pay attention, it is moaning about the word PRIVATE that it is finding on line 14… It is complaining about the word PRIVATE on line 14 but telling you the error is on line 12 and when you double click on this error, it takes you to the comment on line 12 instead of the word PRIVATE that it is complaining about. Why? Because on line 10 I have yet another comment. This means there are two comments in my file before this error occurs and thus the error messages are offset by 2.

It’s quite clear why this happens, though. Just strange that nobody has picked up on this before.

It’s like this weird thing with Unitron since 3.4.0. If you double click a file, it opens up in unitron but it doesn’t show it. It is open, but it is not selected. It shows you the default new page but the page you double clicked on is active in Unitron. As a result you can’t see what you are doing and each time you try to open the file again it just sees the file is already open and doesn’t do anything. I usually have to drag a file into Unitron before I can start double clicking a file to open it otherwise all files are hidden until you manually close the empty default file. It’s weird. Unitron behaves differently ever since Unity was upgraded. Even the earlier versions of Unitron has the same problem. Just now when I was doing this example for you I did the same mistake again… I double clicked a file, it opened in Unitron. I did some changes and noticed my tests didn’t work… Then I realized I was in the wrong script. It didn’t open the right script, it just sent it to Unitron then showed me the last page I was on.

Rather annoying really. If I delete a variable( for instance) in one script and bother script tries to reference it, I would double click on the error in Unity, Unity then opens up the file in Unitron but doesn’t go to the error. I go back to Unity and double click the error again, then it goes to Unitron and goes to the page the error message is in… It used to work so well… now they went and broke it!!! ARgh! How many times have I looked for the error Unity complains about only to realize that Iam in the wrong script and double clicking on the error didn’t take me to the error like it always does, hell not even the right script unless that particular script was already open! How many times have I opened 2 or 4 scripts and all I see is the default empty page… How many times have I opened a file, clicked on the empty page and hit close before I could see my file… Argh! Totally a broken experience. It’s like they are trying to force e to Use the incredibly slooooooow to load Monodevelop…

Completely broke my coding experience this upgrade to 4.3.0. Thought perhaps they fixed it in 4.3.1. No, no they didn’t… I’ll just wait for 4.3.2 and see if they fix what wasn’t broken in the first place… :frowning:



I too prefer Unitron (I’ve been a Smultron user for about 10 years), so hopefully Unity will get these bugs worked out.

As for Pragma Strict being too strict now, I agree… when it’s so strict you can’t even use for loops, I consider it a bug, not a feature!

Err, you can use for loops with pragma strict, it’s pretty simple.

I have found that I can write poor code and not have a problem until runtime without pragma strict.

It would be best to use ‘pragma strict’ to ensure you type variables (for execution speed) etc. It’s harsh but fair.
But is there no global pragma strict? Must we choose it per script using a [barely documented] pragma notation?

Do share.

How to use a normal for loop:

for (i=1; i<=10; i++){}

Without declaring ‘i’ somewhere else in the code or resorting to pragma implicit and pragma downcast?

for (var i : int = 1.......

It’s not harsh, it’s standard practice. The goal is to have as many of your errors as possible be compile time instead of runtime so you can address them there, instead of when the user is trying to use your application.

Wow that’s ugly. And redundant, since you’re already defining it as an ‘int’ with i=1.

But at least it works…

Yes I do actually agree KelsoMRK. It is only harsh to people not writing strict code and typing all their variables.

Which is why there should be a global on/off switch for it. As far as I know this only exists (is on) when doing mobile development though…

It’s explicit, not redundant.