Problem with button press

Hello. I have a small problem at the moment. I think that the issue has to do with the processing speed of the code. The problem is this: When “ButtonPressed” is bigger than “CurWeapon”, the switch does not take place immediately and the weapon that was switched from is still active while the new weapon is too. Whenever “ButtonPressed” is bigger than “CurWeapon”, the switch takes place immediately. Anybody know how I could fix this?

Here is my code which does not work:

void ChangeWeapon(int ButtonPressed, bool reset)
	{
 	Debug.Log("Attempting to change weapon");
    HUD hud = transform.GetComponentInChildren<HUD>();
    bool didWeChange = false;
 	bool canSwitch = false;
    foreach(Weapon wp in WeaponsList)
    {
	if(wp.WeaponKey == ButtonPressed)
	{
	Debug.Log("Button for " + wp.Name + " pressed");
		if(wp.Unlocked == true)
		{
		canSwitch = true;
		Debug.Log("Can switch!");
		}
	}
	if(wp.isCurWeapon == true && reset == false)
		{
		Debug.Log(wp.Name + " is being deactivated...");
		if(canSwitch == true)
			{
			wp.isCurWeapon = false;
       		wp.ActualWeapon.gameObject.SetActive(false);
       		Debug.Log("Deactivated " + wp.Name);
			}
		else
			{
			Debug.LogError("Cannot deactivate");
			}
		}
	else
		{
		Debug.Log(wp.Name + " is not the weapon that is being switched from!");
		}
    if(wp.WeaponKey == ButtonPressed && didWeChange == false)
       {
         if(wp.Unlocked == true)
          {
          Debug.Log("Changing weapon..");
          CurWeapon = ButtonPressed;
		  Debug.Log(CurWeapon + " new CurWeapon");
          LastWeapon = WeaponPlaceHolder;
		  Debug.Log(LastWeapon + " new Last Weapon");
          WeaponPlaceHolder = CurWeapon;
 
          wp.ActualWeapon.gameObject.SetActive(true);
          wp.isCurWeapon = true;
 		  
          Debug.Log("Activated " + wp.Name);
		  
          hud.DisplayCurAmmo = wp.Ammo;
          hud.DisplayCurBulletsInClip = wp.BulletsLeft;
		  hud.DisplayBulletsPerClip = wp.BulletsPerClip;
 		  hud.DisplayMaxAmmo = wp.MaxAmmo;
		  
		  if(wp.NeedsToReload == true)
			{
			NeedsToReload = true;
			}
		  else
			{
			NeedsToReload = false;
			}
          didWeChange = true;
		  break;
          Debug.Log("Weapon Changed");
          }
       }
	}
	}

Anyone know how I could tackle this?

Edit:

I updated the script sample and found out, that the problem is due to order of operations (check image). Notice how the “SMG is being deactivated…” debug is being called before the “Can switch!”. This means that it actually checks what weapon is being deactivated for making sure if it can actually be deactivated. Anybody know how I can prevent this from happening?
[25279-screen+shot+2014-04-16+at+14.36.58.png|25279]

If I’m reading your code right, it looks like you want to loop over each weapon in the WeaponsList looking for the weapon that is unlocked and who’s WeaponKey is the ButtonPressed.

The CurWeapon != ButtonPressed looks to be used to stop checking the WeaponKey if CurWeapon and ButtonPressed match; we can move that out of the loop and use the C# break command inside the loop to stop looping on a match

public void ChangeWeapon(int ButtonPressed)
 {
    if (CurWeapon != ButtonPressed) 
    {
        foreach(Weapon wp in WeaponsList)
        {
           if(wp.Unlocked && wp.WeaponKey == ButtonPressed)
           {
               Debug.Log("Trying to change weapon...");
               CurWeapon = ButtonPressed;
               LastWeapon = WeaponPlaceHolder;
               WeaponPlaceHolder = CurWeapon
               Debug.Log("Weapon Changed");
               break;
           }
        }
    }    
 }

In your second script you didn’t use your wp in foreach and you change your weapon at the first instance where curWeapon is not equal to ButtonPressed, maybe that’s why it acts very quick, by the way you change it (WeaponsList.Count -1) times, add a break after changing because once you changed it you don’t need the rest of the checking. This is definitely faster but I don’t know how your Weapon class looks like so I couldn’t test it:

void ChangeWeapon(int ButtonPressed)
{
	if(CurWeapon == ButtonPressed)
		return;
	
	HUD hud = transform.GetComponentInChildren<HUD>();
	bool didWeChange = false;

	foreach(Weapon wp in WeaponsList)
	{
		if(didWeChange == false && CurWeapon == wp.WeaponKey )
		{
			if(wp.Unlocked == true)
			{
				Debug.Log("Changing weapon..");
				CurWeapon = ButtonPressed;
				LastWeapon = WeaponPlaceHolder;
				WeaponPlaceHolder = CurWeapon;
				
				wp.ActualWeapon.gameObject.SetActive(true);
				wp.isCurWeapon = true;
				
				Debug.Log("Activated " + wp.Name);
				
				if(wp.NeedsToReload) 
					hud.DisplayBulletsPerClip = wp.BulletsPerClip;
				hud.DisplayCurAmmo = wp.Ammo;
				hud.DisplayCurBulletsInClip = wp.BulletsLeft;

				didWeChange = true;
				Debug.Log("Weapon Changed");
			}
		}

		if(didWeChange && wp.WeaponKey == LastWeapon)
		{
			wp.isCurWeapon = false;
			wp.ActualWeapon.gameObject.SetActive(false);
			Debug.Log("Deactivated " + wp.Name);

			break; //we set new weapon and deactivate last one, now it's safe to break
		}
	}
}