I have this weird problem where I have a for inside of a coroutine, and I want to set the classic “int i=0” to be a variable that’s passed to the coroutine, called “time”, but the syntax is wrong so I have to say “time=time”, it works but it says “hey dummy you are saying time is equal time”.
IEnumerator RunTimer(GameObject obj, float time)
{
obj.GetComponent<Timer>().TimerStart.Invoke();
Debug.Log(obj.name);
for (time = time; time > 0; time -= Time.deltaTime)
{
currentTime = time;
yield return null;
}
TimerEnded();
}
I can fix it by making a new “float timer” and change it to “time = timer”, which basically also does nothing.
So is this just a dumb unity alert or is there a correct way of doing this?
It’s just a warning that you’re writing a “strange” assignment because assigning itself to a variable does not make sense as it means nothing essentially, so the compiler is wondering if you meant something else. You can ignore this warning or just use a new variable in the “for” statements.
Or also convert the “for” loop into a “while” logic (best choice imo), for example:
while (time > 0f)
{
currentTime = time;
yield return null;
time -= Time.deltaTime;
}
There is another easier way than transforming the for loop into a while loop. You see, you can omit the initializing in the for syntax. C# won’t complain.