I Need help

I have a problem,

only in the mobile
when the energy reach to 0 It’s doesn’t let me wait
it refill immediately.

but in stimulator in unity
it’s work perfectly.

Let’s take a look at your MainMenu.cs script and hopefully we can figure out what’s going on.

okay

[SerializeField] private Transform massage;

    [SerializeField] private int energymax;
    [SerializeField] private int timetofullenergy;

    [SerializeField] private Button playbutton;

    private int energy;

     private const string energykey = "energy";
     private const string energyreadykey = "energyready";

    [SerializeField] private TMP_Text highscoretxt;
    [SerializeField] private TMP_Text energytxt;

    private void Start()
    {
        onApplicationfocus(true);
    }

    public void onApplicationfocus(bool hasfocus)
    {
        if (!hasfocus) { return; }

        CancelInvoke();

        int highscore = PlayerPrefs.GetInt(scoresys.highscorekey, 0);

        highscoretxt.text = ($" high score: {highscore}");
        //" high score:" + highscore.ToString;

         energy = PlayerPrefs.GetInt(energykey, energymax);

        if(energy == 0)
        {
            massage.gameObject.SetActive(true);
            string energyreadystring = PlayerPrefs.GetString(energyreadykey, string.Empty);

            if (energyreadystring == string.Empty) { return; }

            DateTime energyready = DateTime.Parse(energyreadystring);

            if(DateTime.Now > energyready)
            {
                energy = energymax;
                PlayerPrefs.SetInt(energykey, energy);
               
            }
            else
            {
                playbutton.interactable = false;
                Invoke(nameof(energyrecharge), (energyready - DateTime.Now).Seconds);
            }
        }

          energytxt.text = $"Play ({energy})";
        //"play" + energy.ToString;

            if (energy>0)
            {
            massage.gameObject.SetActive(false);
            }
      
     
    }


    private void energyrecharge()
    {
        playbutton.interactable = true;
        energy = energymax;
        PlayerPrefs.SetInt(energykey, energy);
        energytxt.text = $"Play ({energy})";
    }

    public void play()
    {
        //Debug.Log("TEST");


        if (energy < 1) { return; }

        energy--;

        PlayerPrefs.SetInt(energykey, energy);

        if(energy == 0)
        {
            DateTime oneminutefromnow = DateTime.Now.AddMinutes(timetofullenergy);
           
            PlayerPrefs.SetString(energyreadykey, oneminutefromnow.ToString());


        }

        SceneManager.LoadScene(1);
    }


    public void quit()
    {
        Debug.Log("out!");
        Application.Quit();
    }
}

While there are a few differences, this does look right…
How long is your timetofullenergy set at in the inspector?

just 1 minute

Screenshot_47

As an experiment, try changing that to 10 minutes…

ok…
I try it but nothing changed it’s still 1 minute in unity simulator
and nothing change in mobile still refill immediately.

You changed it in the inspector or the script? Once the value is serialized in the inspector, changes to the value in the codes declaration won’t take affect.

The reason I’m suggesting longer is that one minute is often shorter than the average play session. If it’s 10 minutes, then you can get a better test of the function.

I changed it in the inspector then the script, nothing change at all.

If that’s the case, something is going on that I can’t explain in the slightest (as in, impossible based on the script and the results). I’ll need to take a closer look at the project.

Zip up your project and upload it to https://gdev.tv/projectupload and I’ll take a look. Be sure to remove the Library folder to conserve space.

understood, and sorry for taking your time :raised_hands:.

This one’s a bit strange, and I’m surprised it hasn’t come up more…

DateTime.Seconds returns the seconds part of the DateTime, so 10:30 would return 30
DateTime.TotalSeconds returns the total number of seconds represented by the DateTime, so 10:30 would return 630.
This is complicated, however, because Time.TotalSeconds returns a double, not a float…
Try it this way:

Invoke(nameof(energyrecharge), (float)(energyready - DateTime.Now).TotalSeconds);
1 Like

Thank you so much :heart:, it’s work perfectly
I don’t know if this only happened to me but I’m grateful it’s works

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms