Debugging - Never gets to a line of code while break-pointing

Hi,

I have this code:

void LateUpdate()
	{
		Linker linkedToLinker = linkedTo.GetComponent("Linker") as Linker;
		bool linkedToHasMoved = linkedToLinker.hasMoved;
		bool linkedToHasChangedVel = linkedToLinker.hasChangedVel;
	//here more code but not relevant to the question
	}

When I debug this, and I put a breakpoint right before the first line:

Linker linkedToLinker = linkedTo.GetComponent("Linker") as Linker;

MonoDevelop breaks at that position. So everything fine up to here :stuck_out_tongue:

Then when I put a breakpoint at line 2 (or any line after the first line for that matter):

bool linkedToHasMoved = linkedToLinker.hasMoved;

MonoDevelop never gets here :S

When I put a breakpoint at both the first and second line MonoDevelop breaks on the first one, and when I press f5 then it’ll just continue with the other code and skips the rest of the LateUpdate() method :frowning:

So I suppose there’s something wrong with the first line of code, though I have no idea what :S
I don’t get any compile errors btw :wink:

Any help with this ?

Thanks !
-Pablo

Wrap your code with a try/catch block and try breaking on the catch:

try
{
	Linker linkedToLinker = linkedTo.GetComponent("Linker") as Linker;
	bool linkedToHasMoved = linkedToLinker.hasMoved;
	bool linkedToHasChangedVel = linkedToLinker.hasChangedVel;
}
catch (Exception ex)
{
	throw ex; //break here, normally should just be "throw;", but see notes below
}

Actually, I’ve seen this before, and it might be that the just-in-time compiler is actually optimizing by filtering out your code since it does nothing:

Linker linkedToLinker = linkedTo.GetComponent("Linker") as Linker;
bool linkedToHasMoved = linkedToLinker.hasMoved; //gets hasMoved, but does nothing with it, therefore no need to compile it in
bool linkedToHasChangedVel = linkedToLinker.hasChangedVel; //gets hasChangedVel, but does nothing with it, therefore no need to compile it in

This is just a hunch though.

Unfortunately, I don’t have any experience using MonoDevelop, so this the best I got from general .NET development with Visual Studio.

P.S. the reason I put “throw ex” was just to ensure that the JIT compiler didn’t filter it out :stuck_out_tongue:

P.P.S. if you want to fool the JIT compiler, try doing something with your booleans, like Debug.Log("hasMoved: " + linkedToHasMoved);

I’ll try the try/catch stuff later (since I’m currently in class and can’t do it right now :P)

Though it shouldn’t be that the compiler filters out the bools since i do use them.
The “//here more code but not relevant to the question” part is where I use the bools:

if(linkedToHasMoved == true)
{
//Do stuff here (there actually is code here, so this shouldn't get filtered out either :P
}
else
...

(I also use the linkedToHasChangedVel bool :wink: )

Though I’ll report back when I tried the try/catch thing !

Thanks !

Also, it may be possible that there is a general issue with MonoDevelop/Unity catching the break point, so put:

Debug.Log("EXCEPTION: " + ex.Message + ", TRACE: " + ex.StackTrace);
throw ex;

That way even if you still fail to stop at the break point, at least you know if there’s an exception causing this.

Ok, found the problem
Incredebly stupid :stuck_out_tongue:

first the “linkedTo” variable was a Transform var, but I changed it to a GameObject. I forgot to reassign the variable in the inspector though :stuck_out_tongue:
Though for some reason it first didn’t give an error, though after a restart of unity it did, and then I came to realize what my bug was :smile:

Thanks anyway for all the help ! :smile: