Search Unity

Unity 2D - Need Help with Timer/Finish Line

Discussion in '2D' started by ArtNinJackson, Jan 13, 2020.

  1. ArtNinJackson

    ArtNinJackson

    Joined:
    May 20, 2014
    Posts:
    7
    Howdy!

    I'm pretty new to Unity and I'm trying to learn a very specific solution to a problem that I'm having. I want to build a game where the player must run time trials.

    I have a working script that displays the timer, but I'm having trouble rigging a "finish line" up. I followed some tutorials but they don't seem to be helping (specifically this one:
    ). In the video his timer seems to stop as soon as it connects with the box, but for me, no such thing happens.

    I know there is a difference between Unity in 2D and 3D, so maybe using a 3D box mesh here wouldn't be the best idea. I'm not sure. I'll include my scripts. Any help in this matter is appreciated! I'm also using Unity 2019.1.4f1, if that helps.
     

    Attached Files:

  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    So there may be a couple things here that are an issue. First, this is a 2D forum so if you are doing 3D, you should probably post in the Physics or Scripting forums. If you are using 2D sprites, then your function "OnTriggerEnter" in the FinishLine script should be "OnTriggerEnter2D(Collider2D other)" if you are using a BoxCollider2D or any other 2D collider.

    Also, in the Timer script, you have a function called "Finish" but you never end up calling it. The issue here is that in Update you are checking whether the bool "finished" is true, but it will never be true until you call the Finish() function.

    Another thing, you dont appear to be stopping the timer when Finish() is called. So when you check for finished to be true, even if you do what i advise above, nothing will have because you just have it return.

    Try those suggestions and also add some Debug.Logs in your code so you can debug it easily.

    Last thing, post your code with the "Insert Code" button on the text ribbon when you post/reply in these forums. People will be way more willing to help since they dont have to open something possibly suspicious.

    Hope this helps.
     
    Last edited: Jan 13, 2020
  3. ArtNinJackson

    ArtNinJackson

    Joined:
    May 20, 2014
    Posts:
    7
    Thanks so much for the help! I'll work on that stuff you mentioned above. Sorry for uploading the files! I'm new and wasn't aware! I'm sure I'll be back.
     
  4. ArtNinJackson

    ArtNinJackson

    Joined:
    May 20, 2014
    Posts:
    7
    @Cornysam Thank you so much for your help! I applied your advice this morning and I actually got it working. I did a little more research and was able to expand the functionality of my Timer. I ended up getting rid of the "finish line" script because it was kind of redundant and I was having a hard time connecting the dots between how one script was sending a flag to another, etc.

    Just to clarify, this is in fact a 2D project, and your advice on the "OnTriggerEnter2D" was in fact what was wrong with my script. Also, thanks for pointing out my logical errors. I appreciate the help! Also, just for fun, I think I'll include the code below just so people who need a good timer can borrow from me.

    Thanks again and have a blessed day.
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Timer : MonoBehaviour
    7. {
    8.  
    9.     public Text timerText;
    10.     private float timeLeft = 299.0f;
    11.     private float startTime;
    12.     private bool finished = false;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         startTime = timeLeft;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (finished == false)
    23.         {
    24.             timeLeft -= Time.deltaTime;
    25.             string minutes = ((int)timeLeft / 60).ToString();
    26.             string seconds = (timeLeft % 60).ToString("f0");
    27.  
    28.             timerText.text = "Time: " + minutes + ":" + seconds;
    29.         }
    30.     }
    31.  
    32.     public float stopTime()
    33.     {
    34.         finished = true;
    35.         startTime = timeLeft;
    36.         return timeLeft;
    37.     }
    38.  
    39.     void ResumeTimer()
    40.     {
    41.         finished = false;
    42.         timeLeft = startTime;
    43.     }
    44.  
    45.     void OnTriggerEnter2D(Collider2D coll)
    46.     {
    47.         if (coll == GameObject.Find("FinishLine").GetComponent<BoxCollider2D>())
    48.         {
    49.             stopTime();
    50.         }
    51.     }
    52. }
    53.  
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Good deal! Glad you got it working.