C# Weapon Manager HELP!

So i have a “gun” script i have made for my guns(C#):

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class WeaponMan : MonoBehaviour {
    
    	public GameObject Weapon1;
    	public GameObject Weapon2;
    	public GameObject Weapon3;
    	public GameObject Weapon4;
    	public GameObject Weapon5;
    	public int SelectedWeapon = 0;
    	public GameObject ReloadT;
    
    	// Use this for initialization
    	void Start () {
    		ReloadT.SetActive (false);
    	}
    	
    	// Update is called once per frame
    	void Update () {
    		if(Input.GetKeyDown ("1")){
    			SelectedWeapon++;
    		}
    		if (SelectedWeapon == 0) {
    			Weapon1.SetActive (true);
    			Weapon2.SetActive (false);
    			Weapon3.SetActive (false);
    			Weapon4.SetActive (false);
    			Weapon5.SetActive (false);
    		}
    		if (SelectedWeapon == 1) {
    			Weapon1.SetActive (false);
    			Weapon2.SetActive (true);
    			Weapon3.SetActive (false);
    			Weapon4.SetActive (false);
    			Weapon5.SetActive (false);
    		}
    		if (SelectedWeapon == 2) {
    			Weapon1.SetActive (false);
    			Weapon2.SetActive (false);
    			Weapon3.SetActive (true);
    			Weapon4.SetActive (false);
    			Weapon5.SetActive (false);
    		}
    		if (SelectedWeapon == 3) {
    			Weapon1.SetActive (false);
    			Weapon2.SetActive (false);
    			Weapon3.SetActive (false);
    			Weapon4.SetActive (true);
    			Weapon5.SetActive (false);
    		}
    		if (SelectedWeapon == 4) {
    			Weapon1.SetActive (false);
    			Weapon2.SetActive (false);
    			Weapon3.SetActive (false);
    			Weapon4.SetActive (false);
    			Weapon5.SetActive (true);
    		}
    		if (SelectedWeapon == 5) {
    			SelectedWeapon = 0;
    		}
    	}
    }

Is there anyway to make it so people have to “have” the weapon before they can pick it up? and if they have 2 weapons it swaps the first one picked up with the newer one?

Make a custom weapon script:

public class Weapon : MonoBehaviour
{
	
	private bool _owned = false;
	
	public void Activate()
	{
		
		gameObject.SetActive( true );
		
	}
	
	public void Deactivate()
	{
		
		gameObject.SetActive( false );
		
	}
	
	public bool IsOwned()
	{
		
		return _owned;
		
	}
	
	public void Owned()
	{
		
		_owned = true;
		
	}
	
}

Then a weapon controller script:

public class WeaponController : MonoBehaviour
{
	
	[ SerializeField ] private Weapon[] _weapons;
	
	private int _currentWeapon = 0;
	
	
	void Update()
	{
		
		if( Input.GetKeyDown( "1" ) )
		{
			
			ChangeWeapon();
			
		}
		
	}
	
	void ChangeWeapon()
	{
		
		_weapons[ _currentWeapon ].Deactivate();
		
		do
		{
			
			_currentWeapon = ( _currentWeapon + 1 ) % _weapons.Length;
			
		}
		while( ! _weapons[ _currentWeapon ].IsOwned() );
		
		_weapons[ _currentWeapon ].Activate();
		
	}

}

Also…

[ SerializeField ] private Weapon[] _weapons;

is an array of game objects (your weapons) with the Weapon script attached. You need to assign these objects (drag and drop) to the array variable on the WeaponController game object.

Not sure if you want to automatically change to any new weapon when it is picked up, or just to a weapon that is better. Let me know and I will add the functions to the Controller script.

To switch to a higher _currentWeapon (presumably better) weapon on pick up, just check to see if the _currentWeapon is lower than the new weapon.

To switch to a new weapon on pick up, regardless of its _currentWeapon number, just check to see if it IsOwned(). If ! IsOwned(), then switch to the newly picked-up weapon.