0

I have an achievement that increases with the time survived. I calculate the survived time with Time.time. When I die, I increase the achievement with the Time.time value to increase the seconds survived but it increases much more than it should. My code:

using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;

    public class Timer : MonoBehaviour
    {
        private float StartTime;
        public static float TimerControl;

        void Start()
        {
            StartTime = Time.time;
        }

        void Update()
        {
            //The player is alive
            if (PlayerController.dead == false)
            {
                TimerControl = Time.time - StartTime;
            }

            //The player is dead
            if (PlayerController.dead == true)
            {
                PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_survivor, (int)(TimerControl), (bool success) => {

                });
            }
        }
    }

The amount of survived time that is increased in achievement is much greater than it should.

Any suggestion? Thanks!

  • how much is it incrementing versus how much it is supposed to increment? – vasmos May 06 '20 at 02:41
  • The achievement is to survive 1 hour in total (3600 steps, 1 each second). In the last test I did, I survived 23 seconds and increased more than 8% of the achievement ... It doesn't make sense – Desesperado May 06 '20 at 02:44

2 Answers2

0

Give this a try, if it doesn't work tell me what the debug is giving you.

using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
public class Timer : MonoBehaviour
{
    void Update()
    {
        //The player is dead
        if (PlayerController.dead == true)
        {
        Debug.Log("Time : " + Time.time);
        PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_survivor, (int)(Time.time), (bool success) => {
            });
        }
    }
}

edit: okay I think I know whats happening you are incrementing it on the update loop so you must be implementing it many times

bool recorded = false;
// ...
        //The player is dead
        if (PlayerController.dead == true && !recorded)
        {
            PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_survivor, (int)(TimerControl), (bool success) => {

            });
        recorded = true;
        }
vasmos
  • 2,472
  • 1
  • 10
  • 21
  • Surviving 20 seconds, achievement has been increased by 62%. It doesn't work and the console doesn't show me anything. – Desesperado May 06 '20 at 03:14
  • you are running from editor? if your console doesn't show anything and you have the debug section (white exclamation mark) active and theres no text filter in the search box then this method isn't being called, and your achievement is getting incremented from a different script – vasmos May 06 '20 at 03:18
  • Running from editor the console doesn't show me anything related to the timer. Running from the phone shows me nothing. It's impossible that the achievement is being increased from another script, I have not written it anywhere else – Desesperado May 06 '20 at 03:30
  • In my game I have a timer that shows the seconds and shows them perfectly: GetComponent().text = ((int)TimerControl).ToString(); The timer works perfectly, when I die it stops and even saves the score in an array of Highscores. – Desesperado May 06 '20 at 03:34
  • okay I Think I figured out what happened and I updated my answer – vasmos May 06 '20 at 03:40
0

Have you tried using Time.deltaTime? Or placing that block of code in -

void FixedUpdate() { 
    //code 
}
Gkills
  • 587
  • 1
  • 6
  • 28
Beezee_Boi
  • 61
  • 1
  • 4