ScriptableObjects for Weapons
A recent GameJam gave me a chance to jump into Scriptable Objects for my weapons system. Generally I work with enums to build the framework for reuseable components and shared object parameters, but this worked nicely as well.
Here's a reusable Unity C# script for managing weapon pickup and switching. This script allows your character to pick up different weapons, each with unique attributes like damage, fire rate, and prefab.
Setup Steps:
Create Weapon Scriptable Objects: Define each weapon as a ScriptableObject for easy customization.
Attach Pickup Script to Weapons: Add a trigger collider to each weapon prefab and apply this script.
1. Weapon ScriptableObject (WeaponData.cs)
using UnityEngine;
[CreateAssetMenu(fileName = "NewWeapon", menuName = "Weapons/Weapon")]
public class WeaponData : ScriptableObject
{
public string weaponName;
public WeaponType weaponType;
public float fireRate;
public GameObject weaponPrefab;
public int damage;
public Sprite icon; // Optional: For UI representation
public enum WeaponType { Axe, Knife, Sword, Bow }
}
2. Weapon Pickup System (WeaponPickup.cs)
using UnityEngine;
public class WeaponPickup : MonoBehaviour
{
public WeaponData weaponData; // Assign the weapon data in the inspector
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player")) // Ensure the player has a "Player" tag
{
PlayerWeaponManager weaponManager = other.GetComponent<PlayerWeaponManager>();
if (weaponManager != null)
{
weaponManager.PickupWeapon(weaponData);
Destroy(gameObject); // Destroy the weapon pickup object after pickup
}
}
}
}
3. Player Weapon Manager (PlayerWeaponManager.cs)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerWeaponManager : MonoBehaviour
{
public Transform weaponHolder; // Assign an empty GameObject where weapons will be held
public GameObject currentWeapon;
public WeaponData currentWeaponData;
public void PickupWeapon(WeaponData newWeaponData)
{
// If the player already has a weapon, destroy it
if (currentWeapon != null)
{
Destroy(currentWeapon);
}
// Instantiate the new weapon prefab and set it as a child of the weapon holder
currentWeapon = Instantiate(newWeaponData.weaponPrefab, weaponHolder.position, weaponHolder.rotation, weaponHolder);
currentWeaponData = newWeaponData;
}
// Example method to simulate attacking
public void Attack()
{
if (currentWeaponData != null)
{
Debug.Log($"Attacking with {currentWeaponData.weaponName}, dealing {currentWeaponData.damage} damage.");
}
}
}
How to Use:
Create Weapon Data:
Right-click in the project window.
Select Create -> Weapons -> Weapon Data.
Configure attributes like name, damage, fire rate, and assign a weapon prefab.
Create Weapon Prefabs:
Design a weapon prefab with a trigger (Box, capsule, etc.) and then add a checkbox for "IsTrigger"
Add the WeaponPickup script and assign the WeaponData ScriptableObject.
Setup Player:
Ensure the player has a collider with the tag "Player."
Add the PlayerWeaponManager script to the player.
Create an empty GameObject (e.g., WeaponHolder) as a child of the player, and assign it in the weaponHolder field.
Customizable Features:
You can extend WeaponData with additional attributes like ammo, reload time, or special effects.
Enhance PlayerWeaponManager to switch between multiple weapons or add weapon dropping functionality.
This script allows you to create various weapon pickups with unique characteristics, promoting code reusability and easy weapon customization.














