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.
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.
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?
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
}
}
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
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.
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
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.
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.