- Home /
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?
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.
I can't test it im a complete beginner in javascript and no c#
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; } }
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
@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.
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!
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))
??
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 :)
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# :(
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
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;
}
}
Answer by $$anonymous$$ · Jan 20, 2015 at 10:19 PM
Your best bet would be to:
Make a variable
Set variables to say when that variable occurs
Make the variable usage -=1 every time you press it.
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.
Your answer