I’m trying to do time buffs attached to a button. I have used coroutines and the buff just keep recasting. Might have used it wrong. But I have no idea how to do it. I want it to be subtract cost from my resource. Cast said buff for x amount of time then buff goes away, til I have resource again.
It would help if you showed the code that you used.
I will post code next chance I get on my computer. I have been away from it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Spell2 : MonoBehaviour
{
public MainStuff MS;
public float cost;
public float spelldur;
public float bonus;
public bool isCasted = false;
public Button button;
public Text bonusdis;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
bonusdis.text = bonus + " to Stars Per Sec for " + spelldur;
bonus = (Mathf.RoundToInt(MS.starps + 2 * Mathf.Round(MS.bonus / 20)));
if (MS.star >= cost)
{
button.interactable = true;
}
else
{
button.interactable = false;
}
}
public void spellcast()
{
MS.star -= cost;
isCasted = true;
MS.starps += bonus;
StartCoroutine(casted());
}
IEnumerator casted()
{
yield return new WaitForSeconds(spelldur);
isCasted = false;
}
}
that is the code i am trying. the button works. the bonus works. but the it i cant figure out how to get it to turn off. and also cant figure out how to get it to not be able to be clicked again while the first one is going.
first, Using code tags properly
I assume the button calls “spellcast”?, where do you check if “isCasted” is true before calling it again?, I mean - turning off the players ability to call it should be fine, but a sanity check in spellcast won’t hurt.
IE
public void spellcast()
{
if(isCasted)
return;
MS.star -= cost;
isCasted = true;
MS.starps += bonus;
StartCoroutine(casted());
}
I’m not sure what is going on with the bonus calculation thingy so I won’t comment on that.
the bonus is just random number i am playing with to allow it to grow over time but not scale into stupid. it is just random stuff. not set in stone. and that solves the ability to cast it before it is suppose to be but that still doesnt fix that issue of i click it and it wont turn off.
So if figured out how to do this. Was just putting thing in the right spot.