How do i make a one gun only Weapon system. (2D)

I am working on just a personal project to share with family and friends and it is a 2D platformer. The issue is that I can’t figure out if this code is right and if it is how should I translate it in C#?
The code is: If player walks over weapon, ask player if he wants weapon (in GUI), If he say yes, Move weapon to player hand.
Questions are: Would I need a inventory system for this where I place all the weapons (which are alot) in the player’s inventory but they aren’t accessable (keep in mind this is no rpg so inevntory can’t be accessed) and when player says yes it just unlocks the specific weapon in inventory and disables the current one?
And one last question (srry I am a bit new to this) is there a way I can player spawn with a specific weapon in a every level?
Help is much appreciated!

Hi @TheRealPinecone, the way I do it in my games is to have 2 versions of each weapon:

  • Prefab 1: Kept on the ground and deleted when the player agrees to picking it up
  • Prefab 2: Instantiated directly into the player’s hand when prefab 1 is deleted (picked up)

If you can’t switch weapons that you have picked up then you shouldn’t need to keep track of weapons that you have picked up. Simply delete the instantiated weapon from your hand (second prefab) and instantiate a new version of the weapon you picked up.

Each weapon that you pickup should have an ID (you can keep this in a script for all prefab1 weapons) so that you know what weapon from an array of prefab2s to instantiate.

If you want to start each level with a weapon then just instantiate prefab 2 into the player’s hand.

public Transform playerHand;
//Weapons placed into the prefab arrays should line up: If you have a shotgun in prefab1[0], 
//make sure the player version of the weapon is in prefab2[0]

public GameObject[] prefab1;
public GameObject[] prefab2;

GameObject instantiatedWeapon;

void Start(){
instantiatedWeapon = (GameObject)Instantiate(prefab2[weaponID],playerHand.position,Quaternion.identity);
instantiatedWeapon.transform.parent = playerHand;
instantiatedWeapon.transform.localPosition = Vector3.zero; //Make sure the weapon is in the player's hand
instantiatedWeapon.transform.localRotation = Quaternion.identity; //Make sure the weapon is rotated properly
}

If you want to instantiate a new weapon if a prefab1 is picked up:

void pickUpWeapon(int weaponID){
if(instantiatedWeapon != null){ //If we have a weapon in our hand
Destroy(instantiatedWeapon);
}
instantiatedWeapon = (GameObject)Instantiate(prefab2[weaponID],playerHand.position,Quaternion.identity);
instantiatedWeapon.transform.parent = playerHand;
instantiatedWeapon.transform.localPosition = Vector3.zero;
instantiatedWeapon.transform.localRotation = Quaternion.identity; 
}

@334499p , THANKS SOOO MUCH. Sorry it took me this long to reply, my email didn’t let me open the forum for some reason. But you helped me soo much, it is unbelieveable. This is was one of the only issues I was faceing when creating a platformer, Thanks again, Now I can actually carry on the project.