Home > Net >  Why is my integer value increasing more than once in Unity3D
Why is my integer value increasing more than once in Unity3D

Time:10-04

I am using Unity3D and C# to create a game. I have some code that adds 5 to the coin number when the player wins the level. This is what is consists of:

if (scoreText.scoreNum >= NumberOfBricksNeeded)
    {
        if(HasClickedOk == false)
        {
            LevelFinishedUIs.SetActive(true);

            if (IsPlayingCampaign1 == true)
            {
                coins.CoinNumber  = 5;
                dead.GameOver = false;
                FinishedLevel1UI.SetActive(true);
                Debug.Log("FinishedLevel");
                IsPlayingCampaign1 = false;
                isPlayingCampaign = false;
                HasClickedOk = true;
                One.sprite = OneFin;
                skins.HasEarnedGoldSkin = true;
                skinSaver.Gold = true;
                
                
                campaignSaver.LevelsCompleted = 1;
                
            }

            if (IsPlayingCampaign2 == true)
            {
                coins.CoinNumber  = 5;
                dead.GameOver = false;
                FinishedLevel2UI.SetActive(true);
                Debug.Log("FinishedLevel");
                IsPlayingCampaign2 = false;
                isPlayingCampaign = false;
                HasClickedOk = true;
                TwoA.sprite = TwoAFin;
                skins.HasEarnedMeteorSkin = true;
                skinSaver.Meteor = true;
                
                
                campaignSaver.LevelsCompleted = 2;
            }

Whilst the first one works and adds 5, the second does not. The problem is that it adds 10 to the number instead of 5. I checked the values such as 'HasClickedOk' and 'isPlayingCampaign_' but they are all the appropiate values. Originally, I thought that it was running twice. However, I logged the number of coins to the console and they increased by 10 directly. Not by 5 twice. Is it possible that I have missed something?

Thanks,

CodePudding user response:

The only way you will have 10 added to the coins without explicitly adding to the coins elsewhere, is if IsPlayingCampaign1 & IsPlayingCampaign2 were both true.

Since you don't use if-else, both of the if blocks are being executed after one another adding 5 coins x2.

Make the second if, an else statement.

if (scoreText.scoreNum >= NumberOfBricksNeeded)
    {
        if(HasClickedOk == false)
        {
            LevelFinishedUIs.SetActive(true);

            if (IsPlayingCampaign1 == true)
            {
                coins.CoinNumber  = 5;
                dead.GameOver = false;
                FinishedLevel1UI.SetActive(true);
                Debug.Log("FinishedLevel");
                IsPlayingCampaign1 = false;
                isPlayingCampaign = false;
                HasClickedOk = true;
                One.sprite = OneFin;
                skins.HasEarnedGoldSkin = true;
                skinSaver.Gold = true;
                
                
                campaignSaver.LevelsCompleted = 1;
                
            }

            else if (IsPlayingCampaign2 == true)
            {
                coins.CoinNumber  = 5;
                dead.GameOver = false;
                FinishedLevel2UI.SetActive(true);
                Debug.Log("FinishedLevel");
                IsPlayingCampaign2 = false;
                isPlayingCampaign = false;
                HasClickedOk = true;
                TwoA.sprite = TwoAFin;
                skins.HasEarnedMeteorSkin = true;
                skinSaver.Meteor = true;
                
                
                campaignSaver.LevelsCompleted = 2;
            }
  • Related