Linking Together
Once you’ve added the Barrel and Magazine objects to your weapon prefab, the next step is to connect them so they work as one system. These components need to communicate with each other to handle ammo, firing, and visual feedback correctly.
🔗 Barrel ↔ Magazine Connection
Each barrel object needs a reference to the magazine it will use. This allows the barrel to:
- Read how many bullets are currently available
- Trigger reload logic if needed
- Prevent firing when out of ammo
multiple barrels can use the same magazine
To link them:
- Select your barrel GameObject in the hierarchy.
- In the Barrel component, assign the magazine GameObject to the appropriate field (usually called
Magazine
).
This creates the connection needed for the barrel to read ammo data and respond accordingly.
Without this reference, the barrel won’t know how much ammo it has and will not function correctly.
🔫 Weapon Root ↔ Barrel & Recoil
The main Weapon
script should live on the root of your weapon prefab. This script manages player input and overall weapon state.
To complete the setup:
- Select the root of the prefab.
- In the weapon component, assign the Barrel GameObject to the appropriate field. This tells the weapon when and how to fire.
- (Optional) If your weapon has recoil, assign the Recoil GameObject as well. This allows the weapon to apply visual or animated recoil when firing.
Make sure all references are assigned properly before testing your weapon in the scene.
Once linked, the weapon can:
- Trigger the barrel when input is received
- Tell the magazine to consume ammo or reload
- Apply recoil at the right moment
This linking structure is simple but essential to ensure everything works smoothly.
⏭️ Next Step
That’s is pretty much how to setup a basic weapon prefab that you can config and customize. We have covered the basics so now you can reuse this process to make more complex weapons. Now one thing we didn’t cover is the recoil and how it works.