I am trying to make it so that a button in Unity that can only be pressed and activated every few seconds. I have an array with famous quotes and when I press my button it does display these quotes, but if you press the button continuously then it will cycle through all of the quotes without the ability to be thoughtful and read through them. Now, I have tried something like below, but it did nothing (as in the button still was able to be pressed and no effect was apparent):
Bool pressed;
void Function () {
if (pressed) {
//code to call array
}
}
I thought that a simple combination of bool and an if loop would be sufficient, but this does not work. Am I close to this concept? What would need to be changed to get this operating?
There are a few things you can do to achieve this. Your current code is never toggling the bool
pressed. You can just catch the unwanted button presses with a localized bool like you are doing now, but there might be a better approach.I would consider changing the intractability of the button to also show the end-user that they can not press the button for a short amount of time.
Assign the button in the editor to
btnand set whatever time you want to wait between button presses fortimeToWait. You will also need to assign the button'sOnClicktoClickButton, either in code or in the inspector. With this solution the function will not be able to be called as the button itself will be grayed out and not be able to be clicked. After thetimeToWaitduration is over, the button will reactivate. Place whatever other code you need in theClickButtonmethod that drives your quote cycling.Edit: Here is similar code but using an
Invokeinstead ofCoroutineas requestedEdit 2: There are a few ways you can go about having one button disable all four buttons. In general practice, you do not want these buttons knowing about each other, so it would be a good idea to make a button manager that handles the
OnClicknow and sends the event to allButtonsit knows about.Let me know if you have issues with this.