Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by wakkydude · Feb 24, 2014 at 08:38 PM · javascripttriggernoobif

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");
 }
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image InfiniBuzz · Feb 24, 2014 at 08:38 PM 0
Share

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.

avatar image wakkydude · Feb 24, 2014 at 08:47 PM 0
Share

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?

avatar image highpockets · Feb 24, 2014 at 09:08 PM 0
Share

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.

avatar image highpockets · Feb 24, 2014 at 09:14 PM 0
Share

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;
avatar image highpockets · Feb 24, 2014 at 09:31 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wakkydude · Feb 24, 2014 at 10:20 PM 0
Share

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.

avatar image highpockets · Feb 24, 2014 at 11:59 PM 0
Share

But now your setting the lapTime every frame which is unnecessary processing.... Whatever you want to do, but my way is more optimized

avatar image Dblfstr · Feb 25, 2014 at 12:30 AM 0
Share

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.

avatar image
1

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...
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dblfstr · Feb 24, 2014 at 10:08 PM 0
Share

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.

avatar image wakkydude · Feb 24, 2014 at 10:16 PM 0
Share

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.

avatar image highpockets · Feb 25, 2014 at 12:10 AM 0
Share

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

avatar image Dblfstr · Feb 25, 2014 at 12:28 AM 0
Share

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.

avatar image highpockets · Feb 25, 2014 at 01:41 AM 1
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

If/else statements working one way, and not the other 0 Answers

Gravity Switch by button? 1 Answer

flying should only be possible while fuel 0 < but it continues so long as space is held 2 Answers

Scoring Issue 0 Answers

Having problems with 2d collision triggers (javascript) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges