Machine Gun Control Not Working Properly

Basically, I have a machine gun script, in which when you press the E button when close enough, it will set the gun’s camera to active. Then it sets the player’s camera to not active. It does the opposite after it’s already being controlled. But here’s my problem. I have a single machine gun working (they are mounted). I duplicate the exact gun, and drag it over. When the player tries to use it, it simply keeps the player’s camera on and turns the gun camera on as well, which lets it rotate. I have made sure that all of the variables are the same.

I don’t think this is a script related problem, but here it is:

using UnityEngine;
using System.Collections;

public class ControlMachineGun : MonoBehaviour {
	private GameObject player;
	public GameObject playerCam;
	public GameObject gunCam;
	public GameObject gun;

	public MouseLook ml;

	public bool control = false;

	public Shoot sh;
	// Use this for initialization
	void Start () {
		player = GameObject.Find ("Player");
		sh = (Shoot)gameObject.GetComponent ("Shoot");
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.E)){
			if(Vector3.Distance(player.transform.position, gameObject.transform.position) < 2f){
				control = !control;
			}
		}

		if(control){
			gunCam.SetActive(true);
			playerCam.SetActive(false);
			ml.locked = false;

			sh.controlling = true;
		}else{
			playerCam.SetActive(true);
			gunCam.SetActive(false);
			ml.locked = true;


			sh.controlling = false;
		}
	}
}

Any type of answer would be appreciated. Thanks in advance.

The current way you have it setup, if you go to one of the machine guns and hit “E” and are in distance to execute the if statement(KeyCode one), it will probably enable or do whatever, but the other one(on the other machine gun) will run it’s update and set everything back(because of if(control) due to the same references used for the playerCam(they are the same) used. So control is evaluated to true in one copy of the script, while in the other script it’s evaluated to false. Since the if/else is run each time in the update on both scripts, it will just kind of cancel out what you want it to do.

You may want to have a manager script that exists say on the camera or some main game object that has a set/array/collection/list of these machine guns and isolates the interaction with one out of the list and just doesn’t worry about the others since that isn’t the gameobject/camera child you’re interacting with.

Either way, the issue is same references to playerCam, and opposite conditions for the control variable, the if (control) being eval’d each time.