Search code examples
c#unity-game-enginegoogle-play-gamesachievements

Problem increasing an achievement in Google Play Games in Unity


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!


Solution

  • 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;
            }