Script to only run once on click?

So I have a system where 2 scripts control all interactions, I have my mouse script that uses a raycast to get the GameObject it collides with name and if it is an InteractionBox then it enabled the InteractionInfo script which handles it all

In Interaction there are a few booleans that tell it what scripts are in its parents so it knows what to do once it is activated

The problem is… the way that it works means that when I click the interaction box on a spawner for example it will spawn 2-3 times instead of just once?

public class NMouseController : MonoBehaviour {

	public float LookAtSpeed;

	public bool Firing;
	private InteractionInfo interactionInfo;


	void Start () {
	}

	void FixedUpdate () {

		if (Firing == false) {
			Invoke("LookAtMouse", 0);
		}

		if (Input.GetButton ("CommandKey") && Input.GetButton ("FireKey")) {
			Firing = true;
			Invoke("TargetDetails", 0);
		}

		if (!Input.GetButton ("CommandKey") && Input.GetButton ("FireKey")) {
			Invoke("ClickDetect", 0);
		}

	}

	void ClickDetect(){
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow, 10);
		RaycastHit hit;
		if (Physics.Raycast (ray, out hit)) {
			string HitObj = (hit.transform.gameObject.name);
			if (HitObj == "InteractionBox"){
				interactionInfo = hit.transform.gameObject.GetComponent<InteractionInfo>();
				interactionInfo.enabled = true;
			}
		}
	}

And this is my Interaction script that is called

public class InteractionInfo : MonoBehaviour {

	public Vector3 RayHitPos;

	public bool SpawnScript;
	public bool EnemyAIScript;
	public bool MovementScript;
	private SpawnButton spawnButton;

	private GameObject GameObj;

	void Start(){
		GameObj = transform.parent.gameObject;
		this.enabled = false;
	}

	void Update(){

		if (SpawnScript == true) {
			Invoke ("ActivateSpawn", 0);
		}
	}


	void ActivateSpawn(){
		spawnButton = GameObj.GetComponent<SpawnButton>();
		spawnButton.Spawn = true;
		this.enabled = false;
	}
}

I assume I would go about using Time.time somehow to stop it registering 1 click in 2-3 frames

A couple of things:

  • Input.GetButton Returns true when the button is held down, so if it’s held down across multiple frames it’ll return true each time, so you’ll get the multiple presses behavior. If you want to just see a single press until it’s released and pressed again you want to use Input.GetButtonDown.

  • You don’t want to use input functions inside of FixedUpdate because the input state is reset each frame to deal with things like “button was pressed this frame”. You’ll see intermittent input oddities if you do it in FixedUpdate. So move all of your input handling into Update. Here’s a discussion on it.

If you are actually wanting the “repeat fire while button held” thing, then you’re right, you’ll need to use some sort of timer to space out the shots, otherwise you’ll get one once per frame. Each time you fire a shot, set a timer to 0, each update add Time.deltaTime to the timer, and once it reaches your repeat threshold, fire another shot, reset the time to 0.