if/elseif/else statement not working

 void Update()
    {
        Debug.Log("count");
        Debug.Log(t_dorong3);


 if (2 < rigidBodyFPSController.transform.position.x && rigidBodyFPSController.transform.position.x < 4
            && -1.5 < rigidBodyFPSController.transform.position.z)
        {
            Debug.Log("in the Spot!");
            t_dorong3 = true;
        }
        else if (0 < rigidBodyFPSController.transform.position.x && rigidBodyFPSController.transform.position.x < 1.5
            && -9 < rigidBodyFPSController.transform.position.z && rigidBodyFPSController.transform.position.x < -7.5)
        {
            t_dorong5 = true;
        }

else
        {
           Debug.Log("else");
            t_dorong3 = false;
            t_dorong5 = false;
        }

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.

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”?

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.

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.

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.

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. :frowning:

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

It’s still not working… it still stays the whole time as false

This is what I’m talking about. Be kind to yourself.

var x = rigidBodyFPSController.transform.position.x;
var z = rigidBodyFPSController.transform.position.z;

Debug.Log( "x = " + x + ", z = " + z);

if (x >= 2.0f && x <= 4.0f)
{
  if (z >= 100 && z < 150)
  {
    // something
  }
}
1 Like

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.

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

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.

!(http://help) [quote=“PraetorBlue, post:11, topic: 825034, username:PraetorBlue”]

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.
[/quote]


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

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?

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.

1 Like

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.

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.

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:

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.

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).

Multiple GameObjects, in different positions, with the same script attached.