Start the game only if there is enough energy

I am currently creating energy system for my game. The game takes 5 energy to start the game and 20 energy if it is full . I did everything and they work, just need to restrict the play if energy < 5. Here is code:

void Start()
    {
        Load();
        StartCoroutine(RestoreRoutine());
    }

    public void UseEnergy()
    {
        if (totalEnergy < 5)
            return;
        totalEnergy -= 5;

        UpdateEnergy();

        if (!restoring)
        {
            if (totalEnergy + 1 == maxEnergy)
            {
                //if energy is full just now
                nextEnergyTime = AddDuration(DateTime.Now, restoreDuration);
            }
            StartCoroutine(RestoreRoutine());
        }
    }

    private IEnumerator RestoreRoutine()
    {
        UpdateTimer();
        UpdateEnergy();
        restoring = true;
        while(totalEnergy < maxEnergy)
        {
            DateTime currentTime = DateTime.Now;
            DateTime counter = nextEnergyTime;
            bool isAdding = false;
            while (currentTime > counter)
            {
                if (totalEnergy < maxEnergy)
                {
                    isAdding = true;
                    totalEnergy++;
                    DateTime timeToAdd = lastAddedTime > counter ? lastAddedTime : counter;
                    counter = AddDuration(timeToAdd, restoreDuration);
                }
                else
                    break;
            }
            if (isAdding)
            {
                lastAddedTime = DateTime.Now;
                nextEnergyTime = counter;
            }

            UpdateTimer();
            UpdateEnergy();
            Save();
            yield return null;

        }
        restoring = false;

    }

    private void UpdateTimer()
    {
        if (totalEnergy >= maxEnergy)
        {
            textTimer.text = "Full";
            return;
        }

        TimeSpan t = nextEnergyTime - DateTime.Now;
        string value = string.Format("{0}:{1:smile:2}:{2:smile:2}",(int) t.TotalHours, t.Minutes, t.Seconds);

        textTimer.text = value;
    }

    private void UpdateEnergy()
    {
        textEnergy.text = totalEnergy.ToString();

    }
    private DateTime AddDuration(DateTime time, int duration)
    {
       // return time.AddMinutes(duration);

        return time.AddSeconds(duration);
    }
    private void Load()
    {
        totalEnergy = PlayerPrefs.GetInt("totalEnergy");
        nextEnergyTime = StringToDate(PlayerPrefs.GetString("nextEnergyTime"));
        lastAddedTime = StringToDate(PlayerPrefs.GetString("lastAddedTime"));
    }

    public void Save()
    {
        PlayerPrefs.SetInt("totalEnergy", totalEnergy);
        PlayerPrefs.SetString("nextEnergyTime", nextEnergyTime.ToString());
        PlayerPrefs.SetString("lastAddedTime", lastAddedTime.ToString());
    }

    private DateTime StringToDate(string date)
    {
        if (String.IsNullOrEmpty(date))
        { return DateTime.Now; }

        return DateTime.Parse(date);
    }

Could you give an idea or show me how can I do that?

You already do a check on if total energy is less than 5 and return, so why not just do that same code when you try and play?

It seems like you have already tackled the same issue just for something else, so you already know how to do this. Just check if its less than 5, if it is then return out, if it is not continue onto the “play” part of code.

So the problem is I implement the energy system later than the game. So actually my start button starts an animation and in the animation, game is triggered with an event. So its kind of messy. I didnt want to make it more messy than it is and just wanted to see ideas.

In the handler of that event check if there’s enought energy and if not, “show not enough energy” dialog. Else start the game. Or do the same before starting animation, depends on better user experience.