- Home /
How can i make a button which can be used for only a fixed amount of time and how to display it
I am making a 2d android platformer game and it that there is a button for DASH(which is just normal speed * 5) .The button works perfectly but the thing is I want the button to be used for only about 5 times in the whole level or else the game will be too easy. And also i want to display the number of times left which i can use it......Please help out if anyone knows this
THE CODE FOR DASH IS:
if (dash == true) {
rb.velocity = Vector2.right * dashSpeed;
}
and dash button is on the screen and dashspeed = 5 and i also have a flip function so direction is not a problem for me
Answer by UnityM0nk3y · Apr 14, 2021 at 10:56 AM
Here you go:
You need a button, and a "text" component;
public Text boostAmountText; //Assign in the inspector!!
int boostAmounts;
public Button boostButton; //Assign in the inspector!!
private void Start()
{
boostAmountText.text = "0/5";
boostAmounts = 0;
boostButton.interactable = true;
boostButton.onClick.AddListener(countBoosts);
}
public void countBoosts()
{
boostAmounts += 1;
boostAmountText.text = boostAmounts + "/5";
if (boostAmounts >= 5)
{
boostButton.interactable = false;
}
}
Your answer
Follow this Question
Related Questions
Unity 2D: Properly Implementing Player Movement with a dash, jump, and knockback? 2 Answers
Particle System for Death effect 1 Answer
Adding knockback to a 2D game (in C#) 2 Answers
Blur 2d Sprite 0 Answers
character able to jump non-stop 1 Answer