Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Fix if statement spamming the action

Discussion in 'Scripting' started by ClaysingProductions, Dec 14, 2018.

  1. ClaysingProductions

    ClaysingProductions

    Joined:
    Jul 18, 2018
    Posts:
    39
    Hello,

    I made an input called: "Change face". It is mouse 0. I used this way instead of a button or something cause I want people to customize it. I did a if statement in the update loop to check if the input is called. (it will change the sprite of image) It works alright, but one thing is that I put in some if statements to check the sprite of a image and then it changes to the next one, but it now changes the sprite multiple times.

    Thank you in advance,

    Best regards,

    Jesse
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,335
    Are you using Input.GetMouseButton? That returns true every frame that the mouse button is pressed, so if you are running at 60 frames a second and someone presses the button for a quarter of second, GetMouseButton will be true 15 times in a row.

    Try GetMouseButtonDown or GetMouseButtonUp instead. These two functions only return true once each time the player presses the button.
     
  3. ClaysingProductions

    ClaysingProductions

    Joined:
    Jul 18, 2018
    Posts:
    39
    Hello,

    No I didn't. I did getButton("Change face").

    Best regards,

    Jesse
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,335
    The same thing I mentioned above also applies to getButton. Try getButtonDown or getButtonUp instead.
     
  5. ClaysingProductions

    ClaysingProductions

    Joined:
    Jul 18, 2018
    Posts:
    39
    Hello,

    Thank you very much! It worked!

    Best regards,

    Jesse