- Home /
My if statements are not working [major noob]
Be prepared to pick apart some awful, awful code here. It's a mess right now because I have no idea how to Javascript.
I've been trying to make a fastest lap function for a racing game and my code is just flat out ignoring my if statements. On a more descriptive level, when you run over a button on the ground a door will open (it does) and and you will get a new lap. The game then theoretically checks if your lap time is better than your previous best lap (set by default as 999 to ensure it's always lower). It is doing what's inside the statements but doesn't do the conditional check to actually see if those prerequisites are met - this is also why the door opener and the lap timer are on the same button, because I could not make it for the life of me differentiate between which trigger was what despite using code from this site that was fully working for everyone else that tried it. I'm probably doing something hugely, hugely basic and wrong here and I've not started on my lap limit yet so that's why it may appear to never end. I don't have any concern for players cheating and reversing at this point: I just want this fundamental code working. At the moment my time also isn't resetting to zero whenever I cross the line.
Here's the disaster zone I got going right now. Can someone please disect this and tell me what's gone horribly wrong?
#pragma strict
var Door : GameObject;
var time : float;
var lapTime : float;
var lap: float;
var fastestLap: float;
function Start()
{
lap = 0;
fastestLap = 999;
}
function Update(){
time = Time.time - lapTime;
}
function OnGUI(){
GUI.Box (Rect (10,10,180,25), "current lap time:" + time);
GUI.Box (Rect (10,35,180,25), "current lap:" + lap);
GUI.Box (Rect (10,60,180,25), "best lap:" + fastestLap);
}
function OnTriggerEnter(info : Collider)
{
lap += 1;
if(time<fastestLap);
{
fastestLap = time;
}
time = 0;
Door.animation.Play("Door_Open");
}
function OnTriggerExit(info: Collider)
{
if(info.name == "DoorOpener"); //This line of code currently means nothing and does nothing, it doesn't check and if I name it anything else it will still execute whatever is inside it
yield WaitForSeconds(5);
Door.animation.Play("Door_close");
}
Hi, you have to remove the semicolons (;) at the end of the if statements (end of line) otherwise it will never go into the block. And for the last if-statement you probably need to put brackets around both lines if you want both of them to be executed if the statement is true.
Thanks for letting me get the if statement working! I've also found that line 28 isn't working - I want the time to reset every single time this lap occurs after the check is done, currently it continues ticking but the lap counter still goes up. Is there something I missed there as well?
I see a couple problems.. Lap time is never given a value. Also, time is set to zero in the trigger function, but every update , you're setting time to Time.time - lapTime. Time.time is the seconds from the start of the game and the lapTime is null, so you will be getting some odd return if any at all.
I'm imagining that you want to set the lapTime in the trigger enter function. In this case, you would get the startTime of the lap at the beginning of the lap and then when you enter the trigger, assu$$anonymous$$g that it is at the end of the lap, you would do this:
lapTime = Time.time - startTime;
Your new code is setting lapTime to Time.time on every update which is just the seconds from the start of the game. Get rid of that update function all together because lapTime will be set to zero on the trigger enter and in the very next frame, it will be set to the seconds since the game began.
Answer by Dblfstr · Feb 24, 2014 at 10:11 PM
#pragma strict
var Door : GameObject;
var lapTime : float;
var lap: float;
var fastestLap: float;
function Start()
{
lap = 0;
lapTime = 0;
fastestLap = 999;
}
function Update(){
lapTime += Time.DeltaTime;
}
function OnGUI(){
GUI.Box (Rect (10,10,180,25), "current lap time:" + lapTime);
GUI.Box (Rect (10,35,180,25), "current lap:" + lap);
GUI.Box (Rect (10,60,180,25), "best lap:" + fastestLap);
}
function OnTriggerEnter(info : Collider)
{
lap += 1;
if(lapTime<fastestLap)
{
fastestLap = lapTime;
}
lapTime=0;
Door.animation.Play("Door_Open");
}
function OnTriggerExit(info: Collider)
{
yield WaitForSeconds(5);
Door.animation.Play("Door_close");
}
Here's the solution I needed. I feel really bad for running highpockets around in a circle like this when if I concentrated a whole lot more I would have been able to get his instructions down perfectly the first few times but this just works so I'm gonna have to use it. Thanks.
But now your setting the lapTime every frame which is unnecessary processing.... Whatever you want to do, but my way is more optimized
Yes, I see where setting the time in the update function may not be the best thing to do. I was simply fixing the code he provided. Now, you can have the lapTime shown in your HUD if you like.
Answer by highpockets · Feb 24, 2014 at 09:24 PM
Also, at the top of your OnTriggerEnter function, you should do something like this:
lapTime = Time.time - startTime:
If( lap == 0 )
{
fastestLap = lapTime;
}
If( lapTime < fastestLap )
{
fastestLap = lapTime;
//Do your other stuff...
}
startTime was explained in a comment above by another user. //Do your other stuff means the rest of the code you have written, simply there so he would not have to type out your entire code again for you.
I'm really sorry, I've been fairly stressed this whole time and this being one of my first projects in Unity it's been more me scrambling around trying to make things work and leading the person trying to help me in circles rather than knowing how to implement all of this even if it seems obvious. I was incredibly confused by the do your other stuff mention because of the fact that this kinda overlapped with some of the other lines of code in my program and so it looked like this would have to replace it at some point.
Hey Dblfstr, if you want to check out who posted the startTime option again, you will realize that it was me.. BTW, I was on my iPhone and trying to help the guy out. You gave him an unoptimized version of what should be done and throw a negative comment my way. Clearly, you didn't check who wrote the startTime option above..... Regardless, I'm glad he got it working
No, highpockets. I was responding to a [negative] comment, made by the OP, about how he did not know what you were trying to say. $$anonymous$$y comment was truly in your defense. That comment, though, is gone now. Clearly, you missed it. Have a good one.
EDIT: I'll give you a thumbs up though.
Oh.. I'm sorry Dblfstr.. Thanks for clearing that up.... Now I'm reading your comment again and I feel like such a Douche.. I found it was kind of strange, but missing the comment before threw me off. Very sorry again