Search Unity

Strange if/else behaviour

Discussion in 'Scripting' started by NickMilner, Jul 31, 2014.

  1. NickMilner

    NickMilner

    Joined:
    Jul 12, 2014
    Posts:
    3
    Hi all, can anyone think of a circumstance which would cause execution to flow straight through an if/else statement? I.e.

    Code (CSharp):
    1. if (condition)
    2.   do_something();
    3. else
    4.   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!
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    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
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    are you using braces to scope the clauses? i.e.
    Code (csharp):
    1.  
    2. if (condition)
    3. {
    4.   do_something();
    5. }
    6. else
    7. {
    8.   do_something_else();
    9. }
    10.  
     
  4. NickMilner

    NickMilner

    Joined:
    Jul 12, 2014
    Posts:
    3
    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.
     
  5. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    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.
     
    NickMilner likes this.
  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Have you tried sticking a debug statement immediately above your if/else construct to ensure that this is in fact being reached?
    Code (csharp):
    1. Debug.Log("BEFORE pesky if/else");
    2.  
    3. if (condition)
    4. {
    5.   Debug.Log("    TRUE!");
    6.   do_something();
    7. }
    8. else
    9. {
    10.   Debug.Log("    FALSE!");
    11.   do_something_else();
    12. }
    13.  
    14. Debug.Log("AFTER pesky if/else");
     
    NickMilner likes this.
  7. NickMilner

    NickMilner

    Joined:
    Jul 12, 2014
    Posts:
    3
    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!
     
  8. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    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