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?
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).