Changing weapons script

I working on a FPS game, and i have a problem to change weapons :s

here is the script:

public string CurrentWeapon;
	
	public GameObject M9;
	public GameObject G36C;
	
	void Start() {
		CurrentWeapon = "M9";	
	}
	
	public void ChangeWeapon() {
		
		if(Input.GetKey(KeyCode.Q)) {
			CurrentWeapon = "G36C";
		}
		
		if(Input.GetKey(KeyCode.E)) {
			CurrentWeapon = "M9";
		}
		
		if(CurrentWeapon == "G36C") {
			M9.active = false;
			G36C.active = true;
		}
		
		if(CurrentWeapon == "M9") {
			M9.active = true;
			G36C.active = false;
		}
	}

I have attached the script on FPS controller, then i link the weapons to the script. but still dont change weapons :s

P.S = M9 and G36C are weapons.

Your not calling the function

Add this:

Void Update()
{
ChangeWeapon();
}

I made a Javascript script on this. Create two tags called Pistol and Stick, a terrain, an empty game object, two prefabs, attach the Pistol and Stick tags to the prefabs, attach the code to the empty game object, attach the prefabs to the Pistol and Stick prefab slots, and done!

//Variables
var Pistol : GameObject;
var Stick : GameObject;
var pistolisspawned = true;
var stickisspawned = false;
var canswitchtopistol = false;
var canswitchtostick = true;
	
function Start () {
	ToPistol();
}

function Update () {
	if(pistolisspawned == true) {
		canswitchtopistol = false;
		canswitchtostick = true;
	}
	if(stickisspawned == true){
		canswitchtostick = false;
		canswitchtopistol = true;
	}
	WeaponSwitch();
}

function WeaponSwitch() {
	if (Input.GetKeyDown("q")) {
		ToPistol();
	}
	if (Input.GetKeyDown("e")) {
		ToStick();
	}
}

function ToPistol () {
	if(canswitchtopistol == true) {
	Destroy (GameObject.FindWithTag("Stick"));
	var clone : GameObject;
	clone = Instantiate(Pistol, transform.position, transform.rotation);
	pistolisspawned = true;
	stickisspawned = false;
	}
}

function ToStick () {
	if(canswitchtostick == true) {
	Destroy (GameObject.FindWithTag("Pistol"));
	var clone : GameObject;
	clone = Instantiate(Stick, transform.position, transform.rotation);
	stickisspawned = true;
	pistolisspawned = false;
	}
}

Also, it would make for shorter, simpler code if you just set the weapons active/inactive with the input. Then you would not even need the currentWeapon variable unless you need it elsewhere in your code. Either way, you wouldn’t need those last two if statements.

Here’s my version of this script in C# with some slight adjustments. You’re activating and deactivating objects so you’ll need a third object as a holder for the script since a deactivated object can’t re activate itself. Have the one object you want to start with turned on and the others turned off in the editor.

public class WeaponChange : MonoBehaviour {

public string CurrentWeapon;        //looks for the name of weapons

public GameObject M9;               //Add objects "inventory" style
public GameObject G36C;

void Start()                        
{
    CurrentWeapon = "M9";           //what you're holding
}

void Update()
{
    ChangeWeapon();                 //checks for weapon change input
}

public void ChangeWeapon()
{

    if (Input.GetKey(KeyCode.Q))    
    //On Input turn this one ON and others OFF
    {
        CurrentWeapon = "G36C";
        G36C.SetActive (true);
        M9.SetActive (false);
        
        
    }

    if (Input.GetKey(KeyCode.E))    
    //On Input turn this one ON and others OFF
    {
        CurrentWeapon = "M9";
        M9.SetActive(true);
        G36C.SetActive(false);      
        //You can also add code for sound, particles, etc.
        
    }
}
}