mouse click if (Input.GetMouseButton(0)

hello all,

I am making a coin pusher game, and I have a problem that is boggling my mind I cannot pin point the problem.

I have a coin prefab that I instantiate
//i.e.
** GameObject newCoin = Instantiate(myPrefabCoin, coinDropLeft.GetComponent());**
this is done in the Update() and on a condition:

//if clicked left mouse
** if (Input.GetMouseButton(0)) **
So all is good, the prefab instantiates …but it calls it multiple times. If i do debugging it only gets called once (as I would expect it to) .

I have added an **IEnumerator() **and call it via **StartCoroutine() ** from within the ’ if(Input.GetMouseButton(0)) to try and wait to force a waiting period, i.e. with the hope that i am manually forcing time of say 4 seconds before I call the method to ‘Instantiate(…)’.
I have also looked at mouse settings, thinking it could be something there but nothing is seeming to fix it.

Ive taken it back to be simple, lets just check how many times a click is being done (away from the prefab work) see if the problem is on the click not the prefab instantiation work. So this script below, gets ** DOING** , Before Waiting 2 seconds and After Waiting 2 Seconds mostly 5 times each click but has been between 2-7 Debug.Log(" ") statements on one click. I tried Update and FixedUpdate, just to try ID the prob. I have increased the wait ** yield return new WaitForSeconds(___);** time with no affect at all on the problem (it does of course wait longer :slight_smile: but still get 5 iterations of Logs and instantiations)

using System.Collections;
using UnityEngine;

public class CoinSpawner : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Input.GetMouseButton(0))  
        {
            StartCoroutine(JustWait());
            Debug.Log("Input.GetMouseButton(0)  DOING");
        }
    }



    IEnumerator JustWait()
    {
        Debug.Log("Before Waiting 2 seconds");
        yield return new WaitForSeconds(2);
        Debug.Log("After Waiting 2 Seconds");


    }

}

Input.GetMouseButton returns true as long as the mouse button is held, you probably just need to use this line instead:

if (Input.GetMouseButtonDown(0))  

This will only run once everytime you press the mouse button.