- Home /
my button dosent work, became a light switch instead of a normal button (like a bell)
It's a button that when you click it it auto clicks for you, I added a bool, the idea is that one informs you if you have purchased the upgrade and the other should activate the button so you can click on it again and that would double the price and accumulate for how much you bought(like 0.1 * 5 autoclicks), but it works like a light switch, once turned on it doesn't turn off after you click, so I wanted a way to turn it off after you click it, and you would keep the auto click effect, or if it had, another more effective way to do this same function (to improve the code and such), but im a newby and i dont know how to execute it properly. Can you help me please?
code:
public class Manager : MonoBehaviour { public Text ClicksTotalText;
float TotalClicks;
bool hasUpgrade;
public int autoClicksPerSecond;
public int minimumClicksToUnlockUpgrade;
public GameObject AutoClickButton;
public GameObject ValueText;
private bool isAutoClickActive = false;
public void Start()
{
AutoClickButton.GetComponent<Button>().interactable = false;
}
public void OnAutoClickButtonPress()
{
isAutoClickActive = !isAutoClickActive;
}
public void AddClicks()
{
TotalClicks++;
ClicksTotalText.text = TotalClicks.ToString("0");
}
public void AutoClickUpgrade()
{
if (!hasUpgrade && TotalClicks >= minimumClicksToUnlockUpgrade)
{
TotalClicks -= minimumClicksToUnlockUpgrade;
hasUpgrade = true;
minimumClicksToUnlockUpgrade *= 2;
}
}
public void Update()
{
if (hasUpgrade)
{
TotalClicks += autoClicksPerSecond * Time.deltaTime;
ClicksTotalText.text = TotalClicks.ToString("0");
}
if (TotalClicks >= minimumClicksToUnlockUpgrade)
{
AutoClickButton.GetComponent<Button>().interactable = true;
}
else
{
AutoClickButton.GetComponent<Button>().interactable = false;
}
ValueText.GetComponent<Text>().text = minimumClicksToUnlockUpgrade.ToString();
}
}
Answer by logicandchaos · Oct 03, 2021 at 02:35 PM
if you change hasUpgrade = true; to hasUpgrade = !hasUpgrade; it will make it work like a toggle, click on then click off. Not sure if that is what you want.
Your answer
Follow this Question
Related Questions
Unable to show gameobjects at specific timing 0 Answers
C# Boolean Constantly Changing 1 Answer
Can anyone help me in Bolt 0 Answers
OnTriggerStay not working correctly. 0 Answers
UI button doesn't appear - c# 3 Answers