Problems with if (transform.position)
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?
Answer by miszelfox · Apr 04, 2016 at 06:18 PM
@ashtheartist Remove semicolon after if statement
if (transform.position.y > 3f)
{
print(transform.position.y);
}
Answer by Dave-Carlile · Apr 04, 2016 at 06:17 PM
Semicolon placement is important. You have an extra one somewhere.
Shit didn't even realise, still new I guess. Thanks man :)
Answer by NoseKills · Apr 04, 2016 at 06:24 PM
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;
}
Your answer
Follow this Question
Related Questions
SImple, but how do I check a game object's position in an If statement? 1 Answer
I need to check if a GameObject exists between two values on the y coordiante, how can I do that? 0 Answers
How to check if transform.position reaches target.position? 2 Answers
What is the result of adding transform.position and transform.right? 1 Answer
Why Can't I get 2D object to Move? 1 Answer