- Home /
 
               Question by 
               lastnoob765 · Jul 25, 2021 at 01:30 PM · 
                scripting problemscripting beginnerruntimeforeachguns  
              
 
              If-else statement executing incorrectly in foreach loop,why?
Im making an fps game and i tried to add weapon switching with 1,2,3,4 keys for (primary,secondary,knife,granade). i made this code: . .
 foreach (Transform gun in equipedWeaponsPlaceholder.transform)
                     {
                         if (gun.gameObject.GetComponent<GunScript>().gunType == GunScript.GunType.primary)
                         {
                             audioSource.PlayOneShot(weaponFound);
                             UnactivateAll();
                             gun.GetComponent<GunScript>().isEquiped = true;
                             gun.gameObject.SetActive(true);
                             break;
                         }
                         else
                         {
                             audioSource.PlayOneShot(noWeaponFound);
                         }
                     }
. . Weapon Switches Normaly If it was already in weaponEquipedPlaceholder before runtime.But if i add a weapon after that the code breaks. If i had a glock in weaponEquipedPlaceholder before runtime it plays the weaponFound sound and when i press 2.but during runtime if i add a gun,lets say ak47,from hierarchy or thru code.Then it switches but plays both weaponFound sound and noWeaponFound sounds.That is it runs both if(){} and else(){} even if condition is true.
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                