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 :) 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");
}
}
Answer by I_Am_Err00r · Aug 27, 2019 at 08:57 PM
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.
Anytime!
I don't understand the "Why" so much as to know when to use Update vs FixedUpdate, but Update is better for straight input checks, and FixedUpdate is better for smooth movement calculations, here is someone explaining it, but again I can't really explain the why to use more as the when to use:
Update runs once per frame. FixedUpdate can run once, zero, or several times per frame, depending on how many physics frames per second are set in the time settings, and how fast/slow the framerate is.
It's for this reason that FixedUpdate should be used when applying forces, torques, or other physics-related functions - because you know it will be executed exactly in sync with the physics engine itself.
Whereas Update() can vary out of step with the physics engine, either faster or slower, depending on how much of a load the graphics are putting on the rendering engine at any given time, which - if used for physics - would give correspondingly variant physical effects!
Your answer
Follow this Question
Related Questions
Help I've Lost all Hope, A script stopped working on my Player Character Prefab! 0 Answers
Why does unity not tell me when there is an update 0 Answers
Do Something Once 1 Answer
Inspector doesn't show updated values for public variables (ISSUE or IGNORANCE?) 1 Answer
Just updated to newest Unity version but something's missing 0 Answers