How to instantiate once

Hi sorry for necro post but i couldnt fix it. I know i should use bool to instantiate once but i couldnt find where to put boolean. Thanks for helps

    public void SetScore(int amount)
    {
        _Score += amount;
        scoreText.text = _Score.ToString();
        if (_Score >= 5)
        {
            PlayerPrefs.SetInt("levelReached", 1);
            InstantiateText();
        }

        if (_Score >= 10)
        {
            PlayerPrefs.SetInt("levelReached", 2);
            canInstantiate = true;
            InstantiateText();
        }

        if (_Score >= 15)
        {
            PlayerPrefs.SetInt("levelReached", 3);
            canInstantiate = true;
            InstantiateText();
        }

        if (_Score >= 20)
        {
            PlayerPrefs.SetInt("levelReached", 4);
            canInstantiate = true;
            InstantiateText();
        }
        if (_Score >= 25)
        {
            PlayerPrefs.SetInt("levelReached", 5);
            canInstantiate = true;
            InstantiateText();
        }
    }
    public void InstantiateText()
    {
        if (canInstantiate)
        {
            GameObject unloxText = Instantiate(unlockText);
            Destroy(unloxText, 2);
            canInstantiate = false;
        }
    }

I cant use if(score==x) because sometimes it score up like 3points 7points

if (_Score >= 5 && (PlayerPrefs.GetInt("levelReached") < 1))
{
    PlayerPrefs.SetInt("levelReached", 1);
    InstantiateText();
}

Something along those lines. Effectively you just want to make sure you dont execute it twice. I reused your PlayerPrefs variable, since it’s as good as any other, but you could also have introduced a separate one.

Unless your goal was to make sure you execute each one at least once too, i would also make the conditions mutually exclusive. So a score >=5 but <10 would be one category, and >=10 and <15 would be the next. As is, you are hitting every condition that’s lower than your actual condition (implicit) range as well.

1 Like

@MrBear666 It’s spelled necro, you might want to update your post. And the >= logic is quite incorrect as mentioned in the previous thread with this code. If _Score >= 25 it’s certainly also greater than 5

1 Like

So i change the code like you guys said but now it doesnt instantiate at all i couldnt understand.

        if (_Score >= 5  && _Score < 10 &&(PlayerPrefs.GetInt("levelReached") < 1))
        {
            PlayerPrefs.SetInt("levelReached", 1);
            InstantiateText();
        }

        if (_Score >= 10 && _Score < 15 && (PlayerPrefs.GetInt("levelReached") < 2))
        {
            PlayerPrefs.SetInt("levelReached", 2);
            canInstantiate = true;
            InstantiateText();
        }

thanks i fix it.

Use Debug.Log to find out your runtime variable values and find out exactly why it’s not instantiating. https://discussions.unity.com/t/748729/14

1 Like

Ohh okey im so dump sorry for bothering you guys. When i debug i see its because i didnt reset the player prefs so game is on the max level thats why i cant see lvl up text. Thanks for replies

1 Like

I’m glad you fixed it, and thanks for the follow up!

3 Likes

Not dumb (and certainly not dump :smile:), this happens!
But now you learned the first step towards debugging: gaining new information :slight_smile:

1 Like