PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 21, 2022

[FIXED] Why is my integer value increasing more than once in Unity3D

 July 21, 2022     c#, integer, unity3d     No comments   

Issue

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,


Solution

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;
            }


Answered By - Ermiya Eskandary
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing