How to make it so when player hits ground, the time counter restarts

So I am creating a 2D platformer and the goal is to go as long as possible without dying, and I need to make it so when the player hits the ground, the text displaying the amount of time the player has ran for restarts. I will post the code that I have already written.

Any and all help is greatly appreciated,

Thank you.

Score Manager Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
    public Text scoreText;
    public Text hiScoreText;
    public float scoreCount = 0;
    public float hiScoreCount = 0;
    public float pointsPerSecond = 2;
    public bool scoreIncreasing;
    void Update()
    {
        if (scoreIncreasing)
        {
            scoreCount += pointsPerSecond * Time.deltaTime;
        }
        if (scoreCount > hiScoreCount)
        {
            hiScoreCount = scoreCount;
        }
        scoreText.text = "Score: " + Mathf.Round(scoreCount);
        hiScoreText.text = "High Score: " + Mathf.Round(hiScoreCount);
    }
    public void ResetPlayerScore()
    {
        scoreCount = 0;
        scoreText.text = "Score: 0";
        scoreIncreasing = false;
    }
    public void EnableScoring()
    {
        scoreIncreasing = true;
    }
}

Caller code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Caller : MonoBehaviour {
  ScoreManager _sm;
    void Start()
    {
        // Get reference to ScoreManager in scene
        _sm = GameObject.Find("ScoreManager").GetComponent<ScoreManager>();
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            _sm.ResetPlayerScore(); // sample example of calling a method written in your ScoreManager
        }
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            _sm.EnableScoring();
        }
    }
}

This is a duplicate post?

Do you have any errors? Do the collision messages/methods execute?

I have no compiler errors. It just happens when I hit the ground, the timer doesnt restart.

And my other thread wasn’t getting responses, so I thought I would upload a new thread that could get some attention.

I see… well, I saw your other thread & I thought someone had wrote that they tested it and it was working, so it was getting some attention.

Moving on from that, though, do your messages trigger at all? If you Debug.Log above the if statement, do you see anything in the console?
If you don’t, the collision needs fixing. If you do, perhaps the layer needs fixing… and so on, until you track down the issue :slight_smile:

It did get a response, but I have yet to hear back from said individual, so I thought that I would post another one.

And which “If Statement” should I put it above? All of them?

Well, in the enter/exit methods… outside of checking the layer so you can test each problem by itself. :slight_smile: