I'm trying to do something very basic: "IF value of tranform.position.x equals 4.5 DO something"
Here is my code:
if(transform.position.x.Equals(4.5)){
//(float)transform.position.x == (float)myGM.BottomRightCorner.x
if(directionToMove.Equals(0.1f)){
directionToMove = -0.1f;
print("Direction To Move Changed");
} else{
directionToMove = 0.1f;
print("Direction To Move Changed");
}
}
transform.Translate(new Vector2(directionToMove, 0));
I've tried if(transform.position.x == 4.5)
, if(transform.position.x == 4.5f
, if(transform.position.x.Equals(4.5)
, if(transform.position.x.Equals(4.5f)
and of course I've tried putting the 4.5 in a variable (constant and regular)... but no matter what, when transform.position.x
has a value of 4.5... nothing happens. it completely skips the if statement. I've been programming for 5 hours so far today, so my brain might just have crashed... Just for the sake of it, I did try if(4.5 == 4.5)
just to make sure I wasn't going crazy and somehow wrote the if statement incorrectly... but no, that worked just fine (Thank god...)
I'd greatly appreciate it if anyone can point out any clues as to what I'm doing wrong here.
p.s. I'm aware that my code is not the most efficient thing in the world... I'm trying to see what I can do by myself without following any tutorial. As for what the statement is meant to do... before the transform reaches 4.5, starting from 4.0, increase the transform.position by 0.1f. Once it does reach that, decrease the transform.position by 0.1f.
Basically, move to one side of the screen, then go back to the other side of the screen. Initially, there was a check for the other side, but I stripped this to bare-bones to try and figure it out.
p.s.2: As a side note, this function is called via an "InvokeRepeating()". I'm going to try calling the method in another way, just in case... if that works, I'll remove this post.