Z
Z
Zefirot2021-06-23 14:01:12
C++ / C#
Zefirot, 2021-06-23 14:01:12

What is the best way to change interval with change?

I have several identical prefabs with initially the same properties, but along the way, some of the characteristics change, for example, a counter, I set such a counter

private float RegenerationSpeed = 2f;
void Start(){   
  InvokeRepeating("Regeneration", 0f, RegenerationSpeed);
  }
private void CellRegeneration(){
  ++Counter;
  }
private void SetRegeneration(float r){
  RegenerationSpeed = r;
  }

And now I need it to change when the value changes and the execution speed is
advised to simply bind to Update += Time.deltaTime, they say if the prefabs of the same type will be considered the same
or the same but through FixUpdate

or what is the best way to do it?
The bottom line is that a counter is added to all objects every 2 seconds, but when receiving bonuses, this counter can add, say, 20% faster or vice versa slower, so you need to change the speed dynamically along the way ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mixer1111, 2021-06-23
@Zefirot

I would advise you to do everything with the timer not on the call, but on IEnumerator, everything is nowhere clearer:

IEnumerator test() //Создаёшь новый IEnumerator
{
  //Внутри можешь писать код как в обычном методе
  yield return new WaitForSeconds(<секунды>); //Ждёт нужное количество секунд
}

Thus, everything is even simpler and clearer in my opinion
. And then you create a variable with seconds to wait, and add, depending on something, either ifs, or just like that. Only now you need to make at least one expectation in IEnumerator, or an exception will be thrown
. UPD: I forgot to say that you need to call it not as a function, even though it is essentially a function, but like this:
StartCoroutine(test(/*Ну и аргументы, если нужно*/));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question