Search Unity

if/elseif/else statement not working

Discussion in 'Scripting' started by ob1hnk, Jan 19, 2021.

  1. ob1hnk

    ob1hnk

    Joined:
    Dec 28, 2020
    Posts:
    9
    Code (CSharp):
    1.  
    2.  void Update()
    3.     {
    4.         Debug.Log("count");
    5.         Debug.Log(t_dorong3);
    6.  
    7.  
    8.  if (2 < rigidBodyFPSController.transform.position.x && rigidBodyFPSController.transform.position.x < 4
    9.             && -1.5 < rigidBodyFPSController.transform.position.z)
    10.         {
    11.             Debug.Log("in the Spot!");
    12.             t_dorong3 = true;
    13.         }
    14.         else if (0 < rigidBodyFPSController.transform.position.x && rigidBodyFPSController.transform.position.x < 1.5
    15.             && -9 < rigidBodyFPSController.transform.position.z && rigidBodyFPSController.transform.position.x < -7.5)
    16.         {
    17.             t_dorong5 = true;
    18.         }
    19.  
    20. else
    21.         {
    22.            Debug.Log("else");
    23.             t_dorong3 = false;
    24.             t_dorong5 = false;
    25.         }

    It's in the Update function. I wrote simple Debug.Log scripts to see which if/else statements are running, turns out they're running at the same time. I want it to be if: t_dorong3 == true and else: t_dorong3 == false whereas it stays false from start to end. It's a simple script and I'm probably missing out something obvious. Could I get a little help?

    ++++++++++++++++++
    Console turns out something like this.
    count(314)
    False(314)
    else(314) -> from the else statement
    in the Spot! (83) -> from the if statement


    So I'm confused because it seems the if and else statements are running at the same time.
     
    Last edited: Jan 19, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    I'm having trouble understanding what you're asking here...

    What stays false? One of the variables in this code? Which one? Which statements are running "at the same time"?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Good lord, don't write if clauses like that, they'll NEVER be readable! There are 7 different checks combining magic numbers and random (it seems to be) the .x and the .z component.

    Extract the variables you are questioning to temp variables, do one comparison at a time, use lots of Debug.Log() to figure out what's going on, or else you'll never untangle that mess.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  4. ob1hnk

    ob1hnk

    Joined:
    Dec 28, 2020
    Posts:
    9
    if you see, the t_dorong3 bool is supposed to be true when 'if' runs, and false when 'else' runs. But even when I satisfy the 'if' conditions, it doesn't become 'true'. I wrote Debug.Log scripts in the Update function(to know the total frame number) and in the 'if' and 'else' statement. When I satisfy the if conditions, it runs only the Debug.Log and does not change the t_dorong3 to true.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Put another
    Debug.Log("After if statement, t_dorong3 is : " + t_dorong3);
    after this code. You will see that it is changing as you expect.

    You must have other code that is changing the value.
     
  6. ob1hnk

    ob1hnk

    Joined:
    Dec 28, 2020
    Posts:
    9
    Thank you for your advice about the clauses, but I can't find another way to express whether a gameobject is in a certain area. I searched up magic numbers but I'm kind of in a rush, so what works is what fits. :(

    I edited the question to make it clearer, could you please check one more time?
     
  7. ob1hnk

    ob1hnk

    Joined:
    Dec 28, 2020
    Posts:
    9
    It's still not working... it still stays the whole time as false
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    This is what I'm talking about. Be kind to yourself.

    Code (csharp):
    1. var x = rigidBodyFPSController.transform.position.x;
    2. var z = rigidBodyFPSController.transform.position.z;
    3.  
    4. Debug.Log( "x = " + x + ", z = " + z);
    5.  
    6. if (x >= 2.0f && x <= 4.0f)
    7. {
    8.   if (z >= 100 && z < 150)
    9.   {
    10.     // something
    11.   }
    12. }
     
    ob1hnk likes this.
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    They're not running at the same time. Update runs once every frame. You are getting "in the Spot" during one frame, and then during the next frame (or maybe a later frame, with the "else if" happening in between, you are getting the else.
     
  10. ob1hnk

    ob1hnk

    Joined:
    Dec 28, 2020
    Posts:
    9
    Debug.Log("count"); is in the Update function (and nothing else) which runs every frame, which means the number is the total number of frames run. Which happens to be the number of the times 'else' runs.

    count(314)
    False(314)
    else(314) -> from the else statement
    in the Spot! (83) -> from the if statement

    Also I see 'else' and 'in the Spot' counting up simultaneously in the play mode
     
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Code (CSharp):
    1. Debug.Log("count");
    The only thing this will log to the console is the word "count". I'm not sure where that number "314" is coming from, but I think you're making an assumption that that is the frame count, which isn't reflected anywhere in your code. EIther that or the code you're sharing here is not the same as the code in your project.
     
  12. ob1hnk

    ob1hnk

    Joined:
    Dec 28, 2020
    Posts:
    9

    it's not in the code, it's in the console. The number literally is the frame count because I see it growing every moment in the play mode.

    I tried to post a screenshot of the console window but it's not opening
     
  13. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Then why in this post...
    are you seeing 314, 314, 314 then 83?

    Also that doesn't seem like they're all in the same frame.. you have in the spot and else with different numbers there.

    Are you sure you don't just have more than one object with this same script on it printing different information?
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Probably seeing that because the COLLAPSE feature is enabled in the console window. Turn that off if you're trying to sort out order of operation issues.
     
    PraetorBlue likes this.
  15. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Ah yes! Kurt is completely correct! What you're seeing is not the frame during which these logs are happening.

    What you're seeing is the number of times you have printed that exact log message.

    Turn off Collapse and you will see the logs in order.
     
  16. ob1hnk

    ob1hnk

    Joined:
    Dec 28, 2020
    Posts:
    9
    The 83 was the length of the time I moved my character to satisfy the 'if' statement, therefore executing Debug.Log("in the Spot"). All I wanted to say was that that number(83) plus the 'else' number (314) doesn't add up to the total time number (314) which means the if and else statements were running at the same time.
    Now that I've disabled the collapse feature I don't see numbers but that only makes a difference in expressing the outcome, not my original problem.
     
    Last edited: Jan 19, 2021
  17. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    These numbers are not related to one another, they are just the number of times you saw that log statement. You saw the "if" 83 times, and the "else" 314 times. 314 was not the total frame count.

    If you change your logs to actually show the frame count. You will see that the if and the else are not happening in the same frame:
    Code (CSharp):
    1. Debug.Log("Current frame is " + Time.frameCount);
    If they are happening in the same frame, it's because you have multiple items with this script.
     
  18. ob1hnk

    ob1hnk

    Joined:
    Dec 28, 2020
    Posts:
    9
    What do you mean by multiple items? I tried the actual frame count and it shows the if and else statements are running at the same time(frame).
     
  19. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Multiple GameObjects, in different positions, with the same script attached.