• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by oppyJ · Aug 27, 2019 at 08:26 PM · raycastupdateprefab-instanceupdate problemprefab changing at runtime

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");
 
 
     }
 
 }
 
      
 
 
 
      
 










Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oppyJ · Aug 27, 2019 at 09:08 PM 0
Share

Thanks I_Am_Err00r !

avatar image I_Am_Err00r oppyJ · Aug 27, 2019 at 09:20 PM 0
Share

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:

  1. 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.

  2. 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.

  3. 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

228 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help I've Lost all Hope, A script stopped working on my Player Character Prefab! 0 Answers

,Problem with updating Unity IAP from version 1.23.1 to 1.23.2 0 Answers

Changing prefab instances through script 1 Answer

Can I replace the source image of slices by script? 0 Answers

Unity update broke my particle systems, My scripts are grayed out in the inspector and nothing works. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges