- Home /
Weapon manager
Hello!
I am making a game where you can use multiple weapons. What is the best way to manage these? What i can come up with is just attaching all the diffrent weapon scripts to the playerprefab and only activate the desired weapon script when that weapon is equipped. Can any one give me some pros and cons about this way or perhaps tell me another way that is better?
Thank you
//Taegos
Answer by alexi123454 · Jul 27, 2015 at 08:40 PM
The best way to do something like this would be to create a basic state machine. Create an Enumerator in your main controller script that has all of the various weapon types in it. Then, create a variable that knows what weapon the player is currently using (and potentially what weapon they used last). You could create several functions in the controller script to deal with how the weapons work, or better yet, create a custom weapon class that has methods that you can override for each weapon.
In the controller class, you could just call the "Attack" function on whatever weapon is equipped, and the weapon will the work on it's own. Switching weapons would be as easy as changing the state in the main controller, and updating the script reference for which weapon you're using. This can be done using delegates, or simply just a straight reference to the specific weapon that changes when you switch.
As for storing the information about the weapons, you should probably extend the base weapon script with the different functionality of each weapon (or you could just use copies of the base class if the only difference between them is only damage or something), and create a custom game object for each weapon type (all parented to the player and activated/deactivate when in use/not in use).
Your answer
![](https://koobas.hobune.stream/wayback/20220611233328im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
unlocking weapon (fps) 1 Answer
Detecting Input in the Start function (C#) 1 Answer
Start FPS player off with no weapon 2 Answers
swaping the weapons 2 Answers
Please help me convert this script from using position to rotation? 0 Answers