Code working on Editor, but not on Android - Button, Playerprefs - Can't find solution

I want to block the button when the player see 2 rewarded videos until the next hour, so the button works again. It works fine on Editor, but don’t work on the Android. I tested in 2 androids.

The button starts interactable = true.

Can’t find solution to this problem. Here goes the code:

MenuManager.cs

private int numFreeGem, timeHour, timeDay;


void Start()
{
            // KEY HORA
            if (PlayerPrefs.HasKey("timeHour"))
            {
                tempoHora = PlayerPrefs.GetInt("timeHour");
            }
            else
            {
                PlayerPrefs.SetInt("timeHour", 25);
            }

            // KEY DIA
            if (PlayerPrefs.HasKey("timeDay"))
            {
                tempoDia = PlayerPrefs.GetInt("timeDay");
            }
            else
            {
                PlayerPrefs.SetInt("timeDay", 32);
            }

            // KEY FREEGEMS
            if (PlayerPrefs.HasKey("freeGems"))
            {
                numFreeGem = PlayerPrefs.GetInt("freeGems");
            }
            else
            {
                PlayerPrefs.SetInt("freeGems", 2);
                numFreeGem = 2;
            }


            if (numFreeGem < 1)
            {

                timeTemp = System.DateTime.Now;

                if (timeTemp.Hour != tempoHora)
                {
                    GameObject.FindGameObjectWithTag("FreeGemButton").GetComponent<Button>().interactable = true;
                    PlayerPrefs.SetInt("timeHour", 25);
                    PlayerPrefs.SetInt("timeDay", 32);
                    numFreeGem = 2;
                    PlayerPrefs.SetInt("freeGems", 2);
                }
                else if (timeTemp.Day != tempoDia)
                {
                    if (timeTemp.Day != tempoDia)
                    {
                        GameObject.FindGameObjectWithTag("FreeGemButton").GetComponent<Button>().interactable = true;
                        PlayerPrefs.SetInt("timeHour", 25);
                        PlayerPrefs.SetInt("timeDay", 32);
                        numFreeGem = 2;
                        PlayerPrefs.SetInt("freeGems", 2);
                    }
                }
                else
                {
                    GameObject.FindGameObjectWithTag("FreeGemButton").GetComponent<Button>().interactable = false;
                }
            }
        }
}



        void Update()
        {
            if (numFreeGem < 1)
            {
                timeTemp = System.DateTime.Now;
                if (timeTemp.Hour != tempoHora)
                {
                    GameObject.FindGameObjectWithTag("FreeGemButton").GetComponent<Button>().interactable = true;
                    PlayerPrefs.SetInt("timeHour", 25);
                    PlayerPrefs.SetInt("timeDay", 32);
                    numFreeGem = 2;
                    PlayerPrefs.SetInt("freeGems", 2);
                    //PlayerPrefs.Save();
                }
                else
                {
                    if (timeTemp.Day != tempoDia)
                    {
                        GameObject.FindGameObjectWithTag("FreeGemButton").GetComponent<Button>().interactable = true;
                        PlayerPrefs.SetInt("timeHour", 25);
                        PlayerPrefs.SetInt("timeDay", 32);
                        numFreeGem = 2;
                        PlayerPrefs.SetInt("freeGems", 2);
                        //PlayerPrefs.Save();
                    }
                }
            }
        }




        // Event click
        public void FreeGemBtn_Onlick()
        {
            m_buttonSound.Play();
            AdsControl.Instance.ShowRewardVideo ();
         
        }




        public void GetFreeGems()
        {
            GameData.Instance.SaveData(Constants.GEM, GameData.Instance.GetGemData + 20);
            homeGemText.text = shopGemText.text = GameData.Instance.GetGemData + "";
            numFreeGem--;
            PlayerPrefs.SetInt("freeGems", numFreeGem);
            if (numFreeGem < 1)
            {
                GameObject.FindGameObjectWithTag("FreeGemButton").GetComponent<Button>().interactable = false;
                tempoHora = System.DateTime.Now.Hour;
                tempoDia = System.DateTime.Now.Day;
                PlayerPrefs.SetInt("timeHour", tempoHora);
                PlayerPrefs.SetInt("timeDay", tempoDia);
            }
            PlayerPrefs.Save();
        }

AdsControl.cs

    public void ShowRewardVideo()
    {
        if (Advertisement.IsReady(UnityZoneID))
        {
            ShowOptions options = new ShowOptions { resultCallback = HandleShowResultVideo };

            Advertisement.Show(UnityZoneID, options);
        }

    }

    private void HandleShowResultVideo(ShowResult result)
    {
        switch (result)
        {
            case ShowResult.Finished:
                if (SceneManager.GetActiveScene().name == "Game")
                {
                    FindObjectOfType<PlayerController>().SavePLayer();
                }
                else
                {
                    FindObjectOfType<MenuManager>().GetFreeGems();
                }
                break;
            case ShowResult.Skipped:
                break;
            case ShowResult.Failed:
                break;
        }

    }

Fix your post to use code tags please Using code tags properly - Unity Engine - Unity Discussions

1 Like

Fixed. Thanks!

That helps! Your menumanager I must admit seems overly complex for doing this, especially with the update.

But, one suggestion I have is use Log viewer first to see if you are getting any errors on your tablet

It’s free and easy to use. This would help out first.

As far as the code goes, it may be better to consider that once a player hits two videos, you record a datetime stamp in a playerpref. Then, you can use a coroutine as a timer to countdown the time till the button becomes active again. If you can view ads in multiple scenes, you still use the playerpref and current time to determine how much time should remain. This is just a quick idea.

However, try the log viewer first to see if there is a problem once built out.

1 Like

Yeah, my update is a mess, I pretend to fix or remove it soon. Coroutine is a good idea. Thanks for the tip!
I am going to test the Log Viewer right now.

I am kinda confused, but it is working. To see the logs you just need to make a circle gesture? It didn’t work for me. And I don’t even know if I applied it correctly, but the code is working.

There should be a new menu option (reporter) choose it and select create in your starting scene. Then yes, you draw a circle on the tablet screen (sometimes a few circles quickly) and it should bring up an overlay of the console.

1 Like