Help with enabling and disabling GameObjects with a script C#

I need help with disabling certain game objects. I’m working on a fps and your character has a gun and knife. The script will be attached to you, and the gun and knife will be attached to the main camera, which is attached to you… Here is my script:

using UnityEngine;
using System.Collections;

public class WeaponSwitch : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	GameObject Knife = GameObject.FindWithTag("Knife");
	GameObject Gun = GameObject.FindWithTag("Gun");

	// Update is called once per frame
	void Update () {
		if (Input.GetKey(KeyCode.Z)); 
		    {
			GameObject Knife = GameObject.FindWithTag("Knife");
			GameObject(Knife).SetActive = false;
			GameObject Gun = GameObject.FindWithTag("Gun");
			GameObject(Gun).SetActive = true; }
		if (Input.GetKey(KeyCode.X));
		    {
			GameObject Knife = GameObject.FindWithTag("Knife");
			GameObject(Knife).SetActive = true;
			GameObject Gun = GameObject.FindWithTag("Gun");
			GameObject(Gun).SetActive = false; }

	}
}

Your code in the original post would never compile/work.

If you get references to gameobjects and as long as you don’t destroy them, you should reuse the reference.

Based off what you posted, perhaps this will work for you:

using UnityEngine;
using System.Collections;

public class WeaponSwitch : MonoBehaviour {
	private GameObject knife;
	private GameObject gun;

	// Use this for initialization
	void Start () {
		knife = GameObject.FindWithTag("Knife");
		gun = GameObject.FindWithTag("Gun");
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey(KeyCode.Z));
		{
			knife.SetActive(false);
			gun.SetActive(true); 
		}
			
		if (Input.GetKey(KeyCode.X));
		{
			knife.SetActive(true);
			gun.SetActive(false); 
		}
	}
}

Are those objects placed in the scene and attached to your charector already? Because that’s the error you would get if they arn’t already instanced…