Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
-1
Question by isaacj11 · Jan 18, 2015 at 12:24 AM · buttontouch controlsmaxgui texture

how to limit key press

Hello, Can anybody please tell me how to limit key press, like being able to click on button only for 5 times max, (on a GUI texture) on touch controls?

Comment
Add comment · Show 9
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 richyrich · Jan 18, 2015 at 12:55 AM 0
Share

Within your file have an int variable set to zero.

Each time the button is pressed, add one to the variable.

if the variable gets to a certain number, e.g. 5, don't execute the code behind, or disable/hide the button.

avatar image isaacj11 · Jan 19, 2015 at 07:20 PM 0
Share

I can't test it im a complete beginner in javascript and no c#

avatar image isaacj11 · Jan 20, 2015 at 12:35 AM 0
Share

xxBarginsxx Thanks! this really works as I expected! for a finger exercise we should hit the gui button and the health increases the more you do it the more points! here's my health script, how to link it up to this following limit script you provided? var GuiTexture : Texture; var health = 300;

function Update(){ health -= 2; }

function OnGUI(){ if(GUI.Button(Rect(20,20,health,20), GuiTexture)){ health += 50; } }

avatar image richyrich · Jan 20, 2015 at 10:41 PM 0
Share

Text that was at the end of my (now deleted) answer:

thanks bros! Ill test this out soon and notify you guys, :) and more help please the last one, I've setup a third person, Player Relative Control from standard assets, I've got some script to make my player jump exercise when the screen touched, but when I add Rigidbody the player falls through the ground :( I would only able to test the limit script if this jump works, need help for this, please.

Questions should be clear from the outset as to exactly what you want. Given your question was:

Hello, Can anybody please tell me how to limit key press, like being able to click on button only for 5 times max, (on a GUI texture) on touch controls?

There is severe mission creep here!

Each time you get an answer, you ask for just that little bit more (not tweaks but additional functionality).

1) Limit Clicks

2) Restrict other variables

3) The latest comment

Good luck to others, I'm off

avatar image isaacj11 · Jan 21, 2015 at 07:22 AM 0
Share

@richyrich This is answers.unity3d.com Im a Cgi artist and a complete noob in coding, I can't actually manage anything to get it working, all I want is a limit, 0 to 500 I can't see the another question I've asked, it says your question is waiting to approve by another moderator and im stuck at this jumping, thats why I'm asking here. Thanks for all you've done.

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by xxBarginsxx · Jan 18, 2015 at 02:49 AM

You can do this!enter code here

 For CSharp : 
 
 using UnityEngine;
 using System.Collections;
 
 public class TexturePress : MonoBehaviour
 {
 public int registerKeyPress = 0;
 public int amountOfPressedAllowed = 5;
 
 void Update()
 {
 if (Input.GetMouseButtonDown(0) && amountOfPressedAllowed < 5)
 {
 registerKeyPress ++;
 
 if (Input.GetMouseButtonDown(0) && amountOfPressedAllowed > 5)
 {
 return;
 }
 }
 }
 }
 
 I hope it works! I dont have time to test it but yeah. lets hope for the best!



So yea!

Comment
Add comment · Show 6 · 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 richyrich · Jan 18, 2015 at 02:53 AM 0
Share
 if (Input.Get$$anonymous$$ouseButtonDown(0) && amountOfPressedAllowed < 5)

Given that amountOfPressedAllowed == 5, register$$anonymous$$eyPress++ will never occur ;)

I'm guessing the above code was meant to say:

 if (Input.Get$$anonymous$$ouseButtonDown(0) && (register$$anonymous$$eyPress < amountOfPressedAllowed))

??

avatar image xxBarginsxx · Jan 18, 2015 at 03:42 PM 0
Share

Yes :) I mean to say that better I was very very very tired lol. You got it! :) If you still have trouble, I'll make a script THAT WOR$$anonymous$$S and give it :)

avatar image isaacj11 · Jan 19, 2015 at 06:52 PM 0
Share

Thank you very much but I'm trying to do like a finger exercise game and after a certain amount of time it has to be able to click again, not just 5 times clicking and stop, but continuously clicking again and again but only 5 times and 2 seconds disable and again 5 times. im not able to test the script cuz im a javascript beginner and not C# :(

avatar image rhbrr5hrfgdfgw · Jan 19, 2015 at 07:10 PM 0
Share

wait, so you fixed it or not?

avatar image rhbrr5hrfgdfgw · Jan 19, 2015 at 07:26 PM 0
Share

i dont understand what exactly are you trying to do but what i did understood:

     var curClicks : int = 0;
     var maxClicks : int = 5;
     
     function OnGUI(){
       if (GUI.Button(Rect(0,0,200,200),"Click"){
          if(curClicks < maxClicks && Input.Get$$anonymous$$ouseButtonDown(0))//When you are able to click
              curClicks++;
        }
    }
 
     
     function Update(){
         test();
     }
     function test(){
     
          if(curClicks >= 5){
              yield WaitForSeconds(2);//Waits for 2 seconds.
              curClicks = 0;//Reset
          }
     }

Test if thats what you want

Show more comments
avatar image
1

Answer by xxBarginsxx · Jan 19, 2015 at 08:40 PM

Try this :D

 var resetClickAmount : byte = 0;
 var clickAcum : byte = 0;
 var maxClicks : byte = 5;
 
 var timeTillReset : float = 2f;
 
 function OnGUI()
 {
     if(GUI.Button(Rect(10, 10, 256, 32), "You clicked " + clickAcum + " Times!"))
     {
         clickAcum ++;
         
         if (clickAcum >= maxClicks)
         {
             ResetClickAcum();
         }
     }
 }
 
 function ResetClickAcum()
 {
     if (clickAcum >= maxClicks)
     {
         yield WaitForSeconds(timeTillReset);
         clickAcum = resetClickAmount;
     }
 }
 
Comment
Add comment · Show 1 · 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 xxBarginsxx · Jan 20, 2015 at 06:56 PM 0
Share

Yes I will :D And give a better one :D

avatar image
0

Answer by $$anonymous$$ · Jan 20, 2015 at 10:19 PM

Your best bet would be to:

  1. Make a variable

  2. Set variables to say when that variable occurs

  3. Make the variable usage -=1 every time you press it.

Comment
Add comment · 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
0

Answer by daipayan123 · Jan 21, 2015 at 08:02 AM

maybe you could use a counter to track the number of button presses and have a flag associated with it. If the counter exceeds 5, the set the flag and do what you want to do.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How to make the button respond to touch and hold feature? 5 Answers

Android Touch Screen Button for touch and hold 1 Answer

How to create a sprite button to load level when touched 0 Answers

Show/hide GUI Texture 1 Answer

Two buttons (Quads) are not working at a same time. 2 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