Chapter 9: apparent error in the GunEquipper script

I have the latest PDF version of the book. There is a script given for switching guns, that does not allow for switching to the Assault Rifle. I edited my code to allow for it, but others may encounter this issue. Below is what is in the text.

void Update ()
{
if (Input.GetKeyDown(“1”))
{
loadWeapon(pistol);
activeWeaponType = Constants.Pistol;
loadWeapon(assaultRifle);
activeWeaponType = Constants.AssaultRifle;
}
else if (Input.GetKeyDown(“3”))
{
loadWeapon(shotgun);
activeWeaponType = Constants.Shotgun;
}
}

1 Like

@anthonyuccello Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

Hi @ryanbowles42

Thanks for pointing this out. I’ve made a fix locally so it will go out in the next update.

Thankfully, if you continue a little further in the chapter, you will find the correct code is there. It looks like the first time this code block is shown it was missing one of the conditions. It should be:

void Update() 
{
  if (Input.GetKeyDown("1")) 
  {
    loadWeapon(pistol);
    activeWeaponType = Constants.Pistol;
  } else if (Input.GetKeyDown("2")) 
  {
    loadWeapon(assaultRifle);
    activeWeaponType = Constants.AssaultRifle;
  } else if (Input.GetKeyDown("3")) 
  {
    loadWeapon(shotgun);
    activeWeaponType = Constants.Shotgun;
  }
}

Sorry for the confusion this might have caused, and thanks for bringing this up here.

1 Like

Yeah I came across this as well, maybe worth correcting in future versions but the correct code is there further on :slight_smile:

1 Like

I was going to comment just that!
It’s missing one “else if” block, it should be one for pistol, one for Assault rifle and finally one for the shotgun! :slight_smile:

Thanks for the heads up!

This topic was automatically closed after 166 days. New replies are no longer allowed.