C# Practice: Switching Weapons

Sabrina Windsor
Jul 14, 2021

--

Objective: Create a switch statement that will check a value obtained in game mode to select a weapon in the console.

For this one we’ll want to create a script and attach it to an object so we can see the component in the inspector. Then we want to make a public int variable so that we can change its value while we’re in game. We can name this variable, weaponID.

Now the switch statement is fairly simple, we just need to pick a number of weapons and create a case number for each one.

That’s all we need to get it working. In game, you can change the weaponID number to 1, and you’ll get the gun message, and so on. If you don’t have a number that matches a case, the default message will appear.

--

--

Sabrina Windsor
Sabrina Windsor

Written by Sabrina Windsor

Currently learning to code with the help of GamedevHQ in order to someday my my game ideas come to life!

No responses yet