C# Add game object to current list value

I am working on a weapon switching/pickup system for my FPS and am mostly finished.
I currently have 3 weapons (the player can hold 2 at one time) but when I pick up a weapon it adds it to the list value of 1 even when the player is currently holding the weapon in position of 0, the gun picked up goes to list value 1 while the other existing gun goes down to (or stays at) list value 0. Is there any way to add the gun to the current list value? I have a public int in my code called ‘arrayPos’ that tracks which list value i am currently on (i change with the scroll wheel) and has the value stored if this helps? Any Help???

Thank YOU!

public int arrayPos = 0;

private void Update() {

     if (arrayPos == 0 && weapons.Count > 0)
     {
         weapons[0].SetActive(true);
         currentWeapon = weapons[0];
         if (weapons.Count>1)
         {
             weapons[1].SetActive(false);
         }
       
     }
     else if (arrayPos == 1 && weapons.Count > 0)
     {
         weapons[1].SetActive(true);
         currentWeapon = weapons[1];
         if (weapons.Count > 1)
         {
             weapons[0].SetActive(false);
         }

     }

     //currentWeapon.SetActive(true);

     if ((Input.GetAxis("Mouse ScrollWheel") > 0f) && weapons.Count > 1)
     {
         if (arrayPos >= 1)
         {
             arrayPos = 0;
         }
         else
         {
             arrayPos += 1;
         }
     }
     if ((Input.GetAxis("Mouse ScrollWheel") < 0f) && weapons.Count > 1)
     {
         if (arrayPos <= 0 )
         {
             arrayPos = 1;
         }
         else
         {
             arrayPos -= 1;
         }
     }




     if (Range_AK47 && Input.GetKeyDown("e") && weapons.Count < maxWeapons && weapons.Contains(AK47) == false)
     {
         weapons.Add(AK47);
     }

     if (Range_AK47 && Input.GetKeyDown("e") && weapons.Count <= maxWeapons && weapons.Contains(AK47) == false)
     {
         currentWeapon.SetActive(false);
         weapons.Add(AK47);
         weapons.Remove(currentWeapon);
      

     }



     if (Range_M4 && Input.GetKeyDown("e") && weapons.Count < maxWeapons && weapons.Contains(M4) == false)
     {
         weapons.Add(M4);
     }

     if (Range_M4 && Input.GetKeyDown("e") && weapons.Count <= maxWeapons && weapons.Contains(M4) == false)
     {
         currentWeapon.SetActive(false);
         weapons.Add(M4);
         weapons.Remove(currentWeapon);
         

     }


     if (Range_Pistol && Input.GetKeyDown("e") && weapons.Count < maxWeapons && weapons.Contains(Pistol) == false)
     {
         weapons.Add(Pistol);
     }

     if (Range_Pistol && Input.GetKeyDown("e") && weapons.Count <= maxWeapons && weapons.Contains(Pistol) == false)
     {
         currentWeapon.SetActive(false);
         weapons.Add(Pistol);
         weapons.Remove(currentWeapon);
       

     }
 }

There are some logic issues in that, for sure.

first, not correlated with your issue, this first if could be a lot simpler

   if ( weapons.Count > 0)
     {
          weapons[arrayPos].SetActive(true);
          currentWeapon = weapons[arrayPos];
          if (weapons.Count > 1)
          {
              weapons[arrayPos].SetActive(false);
          }
 
      }

Now for the real problem, when you add a new weapon to the weapons list you do not change the arrayPos value.

I believe that you should always set it to the last index of your list, that is the index of your most recently picked up weapon. It’s easier than trying to add the weapon to an index that is not the last. Something like this :

arrayPos = Mathf.Max(weapons.Count() - 1,0)

And as last comment, the if’s you use to add weapons could be simpler and more readable as well, and i really think there is bug there. The only difference between the criterias is that the second one for each weapon has an equal sign in addition to the “minor than” that there was in the first one. So, if the first ‘if’ is true, the second one will also be. I would change it to be like this

      if (Range_Pistol && Input.GetKeyDown("e") && weapons.Count < maxWeapons && !weapons.Contains(Pistol))
      {
          weapons.Add(Pistol);
      }
      else if (Range_Pistol && Input.GetKeyDown("e") && weapons.Count  = maxWeapons && !weapons.Contains(Pistol))
      {
          currentWeapon.SetActive(false);
          weapons.Add(Pistol);
          weapons.Remove(currentWeapon); 
        
      }
     // adding the logic to change arraypos here