Should be a really easy fix but I’m not too sure what I’m doing wrong.
if (transform.position.y > 3f);
{
print(transform.position.y);
}
On JS, I want the console to print the Y axis once the game object this script is attached to goes above 3.0 (on the Y axis).
The console immediately prints the Y axis even though the game object has not gone above 3. This also happens per frame when in the function Update.
Any ideas on what I could be doing wrong?
Semicolon placement is important. You have an extra one somewhere.
@ashtheartist Remove semicolon after if statement
if (transform.position.y > 3f)
{
print(transform.position.y);
}
The most loops as well as the if-condition affect the code block defined by curly brackets or until the next semicolon if you don’t use curly brackets. Since you have a semicolon at the end of the if-line, the if-block is empty.
if (transform.position.y > 3f) [ this code is affected by the if ] ;
{ // this is just code inside curlies without any conditions
print(transform.position.y);
}
If you want to print it just once, there’s no magic trick. Make a variable that prevents printing it again.
bool printed;
...
if (!printed && transform.position.y > 3f)
{
print(transform.position.y);
printed = true;
}
@miszelfox lol that was easy good job!