call function at a time interval within update

When i click and hold the mouse, it is supposed to show me the GameObject’s position where my mouse is pointing at (while clicked) at a repeat rate of 1.

However, what I am getting is the debug.log is being called several times (70+) even if i clicked once!

bool Activate = false;

void FixedUpdate(){
	StartCoroutine (testing (1.0f));
}

IEnumerator testing(float waitTime) {
	Activate = true;
	yield return new WaitForSeconds (waitTime);
	if (Input.GetMouseButton (0)) {
		RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);

		if (hit.collider != null) {
			Debug.Log (hit.transform.position.ToString ());
		}

		Activate = false;
	}
}

I think you’re calling your Coroutine every time your physics refresh because it’s being called in FixedUpdate. Try calling your coroutine only once via Start.

Im not sure why you need the additional timer. Fixed update is a timer itself.

	float timer;
	void FixedUpdate () {
		timer += Time.deltaTime;
        if (timer > 1) { timer -= 1;
        if (Input.GetMouseButton (0)) {
						print ("one second passed....raycast here");}
				}
	
	}

@Fr33zerPop is right, you are starting a new coroutine several times by frame. If you want use a coroutine for this, you should initialize one time in Start or more complicated like this

   Coroutine _showGameObjectPosition;
    
    void Update(){
        
        if (Input.GetMouseButtonDown (0)) _showGameObjectPosition = StartCoroutine (testing (1.0f));
        if (Input.GetMouseButtonUp (0)) StopCoroutine (_showGameObjectPosition);
    }
    
    IEnumerator testing(float waitTime)
    {
        var wait = new WaitForSeconds(waitTime);
        
        while(true) {
            
            RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
            
            if (hit.collider != null) {
                Debug.Log (hit.transform.position.ToString ());
            }
            
            yield return wait;          
        }
    }

But if is only a timer i recommend the method that show @toddisarockstar