英文:
Enabling button after 100 clicks
问题
我想在我的游戏中创建一个秘密或者什么东西。基本上我想问的是,如何在点击按钮100次后使按钮起作用。点击100次后,它应该给我100个金币。我是Unity的新手,所以如果你能更精确地解释一下就好了。我尝试了这段代码:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class SecretButton : MonoBehaviour
{
public int clickcounter = 0;
public TMP_Text scoreText;
int moneyAmount;
void Start()
{
}
void Update()
{
if (clickcounter == 100)
{
moneyAmount += 100;
PlayerPrefs.SetInt("MoneyAmount", moneyAmount);
moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
scoreText.text = moneyAmount.ToString();
}
}
public void increaseClicks()
{
clickcounter++;
}
}
但它一直给我100个金币,但我只想得到100个一次。
英文:
so I wanted to create a secret or something in my game. Basically what i wanted to ask is how do you make a button work after clicking it a 100 times. After clicking a 100 times it should give me 100 money. Im new to unity so it would be nice if you could explain it more precise. I tried this code:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class SecretButton : MonoBehaviour
{
public int clickcounter = 0;
public TMP_Text scoreText;
int moneyAmount;
void Start()
{
}
void Update()
{
if (clickcounter == 100)
{
moneyAmount += 100;
PlayerPrefs.SetInt("MoneyAmount", moneyAmount);
moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
scoreText.text = moneyAmount.ToString();
}
}
public void increaseClicks()
{
clickcounter++;
}
}
but it started giving me 100 money all the time, but i wanted it to give me only once 100 money.
答案1
得分: 0
Easiest way I can think of is creating a variable _counter assigned to the button and increase it by one for each button press.
inside the button press function, include an if statement that checks whether the _counter > 100 and in that case it starts the followup actions, otherwise it skips the action.
The answer is quite high level, but if you want more details, you have to share more details about what you are trying to accomplish
update:
I can see in your edit the code now, your additional problem with giving 100 money multiple times is because you don't make this function execute only once. You can solve this by adding another variable _secret_available and initiate it as True, when the function is activated, set the variable to False.
You also need to edit the if clause to be like this:
if(counter ==100 && _secret_available)
Also my suggestion would be to replace counter==100 with counter >= 100 so that you don't have possible bugs in case the counter jumps from 99 to 101 without passing by 100. Might not be a problem in the moment but I believe it is more robust
英文:
Easiest way I can think of is creating a variable _counter assigned to the button and increase it by one for each button press.
inside the button press function, include an if statement that checks whether the _counter>100 and in that case it starts the followup actions, otherwise it skips the action.
The answer is quite high level, but if you want more details, you have to share more details about what you are trying to accomplish
update:
I can see in your edit the code now, your additional problem with giving 100 money multiple times is because you don't make this function execute only once.
You can solve this by adding another variable _secret_available and initiate it as True, when the function is activated, set the variable to False.
You also need to edit the if clause to be like this:
if(counter ==100 && _secret_available)
Also my suggestion would be to replace counter==100 with counter >=100 so that you don't have possible bugs in case the counter jumps from 99 to 101 without passing by 100. Might not be a problem in the moment but I believe it is more robust
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论