Hi all, can anyone think of a circumstance which would cause execution to flow straight through an if/else statement? I.e. Code (CSharp): if (condition) do_something(); else do_something_else(); When I run in debug it executes do_something() because condition is true, and then goes right on and executes do_something_else() as well! It's really infuriating! Something else that tells me all is not well is that I also cannot step into either do_something() or do_something_else() even though both are in the same source file. In fact, if I try putting a breakpoint in there it shows up in a pale pink colour and doesn't work either. I've tried re-sycing, deleting the mono cache and re-importing everything. Any ideas? Thanks!
Do you have this on multiple objects where a condition can be true/false? Maybe it's true on 1 and false on another, do a Debug.Log(gameObject.name) to see what object is currently being checked. Otherwise it's impossible
are you using braces to scope the clauses? i.e. Code (csharp): if (condition) { do_something(); } else { do_something_else(); }
The scene consists of the Main Camera and a single empty object containing the script. I've confirmed that this is indeed the object being executed (not that it can be anything else). There is only one statement in the if and else blocks so braces aren't necessary, but I've tried it with braces as well and the behaviour is the same. > Otherwise it's impossible I agree. That's what makes it infuriating! BTW, I found a reference to the breakpoint problem I'm experiencing here: http://answers.unity3d.com/questions/587981/why-does-monodevelop-sets-pink-nonfunctioning-brea.html The thread was closed as off-topic.
I think this is a problem with MonoDevelop's debugger, and it is not actually executing both statements. The MonoDevelop cursor just gets easily confused and doesn't know what it's pointing to. If you put Debug.Log() statements in those functions to say "I'm in do_something!" and "I'm in do_something_else!", you'll probably see that it isn't actually executing both of them; the debug cursor is just pointing to the wrong place.
Have you tried sticking a debug statement immediately above your if/else construct to ensure that this is in fact being reached? Code (csharp): Debug.Log("BEFORE pesky if/else"); if (condition) { Debug.Log(" TRUE!"); do_something(); } else { Debug.Log(" FALSE!"); do_something_else(); } Debug.Log("AFTER pesky if/else");
Thanks to both makeshiftwings and numberkruncher! It was just the debugger that was confused. I'll search around and see if there's a solution to this or, alternatively, it might be time to switch over to Visual Studio. Thanks!
eurghh monodevelop then. If you didn't know, UnityVS is now released free as an official plugin for Visual Studio. Get it while it's hot