Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do I make this countdown timer currency work properly?

edited July 2020 in Programming

So I am making a game similar to Dancing Line. And I want to add a currency that when the currency is not at its max, a timer shows and the countdown begins and refills 1 currency per 30 seconds. It is not working as it just goes back to 15/15 currency instead of adding 1. I also wanted it to countdown when player closes the game too.


using System.Collections;

using TMPro;

using UnityEngine;


public class CubeTimer : MonoBehaviour

{

  public GameObject timer;

  public TMP_Text time;

  public float seconds;


  public float cubes;


  public bool isMissingCubes;


  private void Start()

  {

    timer.SetActive(false);

    seconds = PlayerPrefs.GetFloat("seconds", 30);

  }


  private void Update()

  {

    cubes = PlayerPrefs.GetFloat("cubes", 15);


    if (seconds < 9)

    {

      time.text = "00:0" + seconds.ToString("F0");

    }

    else

    {

      time.text = "00:" + seconds.ToString("F0");

    }


    if (cubes < 15)

    {

      isMissingCubes = true;

    }


    if(cubes >= 15)

    {

      isMissingCubes = false;

      PlayerPrefs.SetFloat("seconds", 30);

    }


    if (isMissingCubes == true)

    {

      timer.SetActive(true);

      seconds = PlayerPrefs.GetFloat("seconds", 30);

      seconds -= Time.time;


      if (seconds < 9)

      {

        time.text = "00:0" + seconds.ToString("F0");

      }

      else

      {

        time.text = "00:" + seconds.ToString("F0");

      }


      if (seconds < 0)

      {

        cubes += 1;

        PlayerPrefs.SetFloat("cubes", PlayerPrefs.GetFloat("cubes") + 1);


        seconds = 30;

        PlayerPrefs.SetFloat("seconds", 30);


        if (seconds < 9)

        {

          time.text = "00:0" + seconds.ToString("F0");

        }

        else

        {

          time.text = "00:" + seconds.ToString("F0");

        }

      }

    }

    else

    {

      timer.SetActive(false);

    }


    Debug.Log(seconds);

  }

}

Best Answer

Sign In or Register to comment.