Calling two Methods on change variable ( Up & Down)

What’s up! I’m stuck with an idea that I can’t program.
I have a variable that increases from 1 by 1, that is, each time an action is performed, 1 is added to it. But if the Player receives a hit, he loses everything and returns to zero … Every time he adds 1, a coroutine is executed. And every time it is reset to 0, another one has to do so. This would not be a problem if it were not because as I check the status of my variables in Update the actions (placed in coroutines) are repeated continuously as long as the value remains and yet I want the actions to only be executed ONCE but EVERY TIME that happens.
I know probably there´s a stupid solution but sincerely I really appreciate any help.
I tryied with a bool condition in coroutines that “switchof” themselves when finishing but it didn´t work :frowning:

IEnumerator Action ()
{
if condition
{
//do something
condition = false;
yield return null;
}
}

That works once but not as desired.

Can you post the complete script?
It’s difficult to help you just with that snippet.
My idea is that you probably do not need the coroutines at all.

2 Likes

Yeah, definitely stay away from coroutines for this… this doesn’t even sound remotely like anything that needs a coroutine.

Keep it simple. Make a counter integer:

public int GoodieCounter;

Place this where the player does his action:

GoodieCounter++;

Place this where the player gets hit:

GoodieCounter = 0;

Now if you want to display this to a score on the screen:

public UnityEngine.UI.Text GoodieCounterText;  // drag this in from the inspector

And in the Update() method:

GoodieCounterText.text = GoodieCounter.ToString();

Note the conspicuous lack of coroutines.

Sorry…yes i gonna try to explain it better

Probably I didnt explain myself properly. This is not what I´m looking for…Thanks anyway :slight_smile:

Ok
Imagine the player shoots enemies. If I killed one we add a point to the counter.
If one escapes the counter goes to zero again and starts counting the enemies I kill.
If I kill 5 enemies in a row without them escaping then I change my weapon (every 5 enemies)
But if only one escapes my counter goes to zero but I don’t lose my weapon.
So I start counting again…0,1,2,3…
If enemies killed me then I lose my weapons and start again.

So I have two counters…one temporal…0,1,2,3,4,5…and one that stores this counter because it goes to ZERO the store counter doesn´t. So the progression is like this:
TEMPORAL : 0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,8,9,10,11,12,13,…
STORE : 0,1,2,3,4,5,6,7,7,7,7,7,7,7,7,7,8,9,10,11,12,13,…

Then I have a weapon status where I save the state of weapons I have (like first 5 = granade, then 10 = doble granade, then 15=dinamyte, and so on)

So :
TEMPORAL : 0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,8,9,10,11,12,13,…
STORE : 0,1,2,3,4,5,6,7,7,7,7,7,7,7,7,7,8,9,10,11,12,13,…
W.STATUS : 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2 ( I can get this with the Mathf.Floor of STORE /5)

All this must be counted in the Update Method as they change in playtime.

Now, if we say TEMPORAL is T, STORE is S and W.STATUS is W to abreviate, then:

if T=5, S=5 and W=1 then I need to execute a Courutine ONLY once (is because I need to run it with WaitforSeconds between some actions)
If I write something like:

IEnumerator Action ()
{
if condition
{
//do something
condition = false;
yield return null;
}
}

Works the first time because CONDITION starts like true and when ACTION finish makes CONDITION FALSE.

But how can I do CONDITION TRUE again to make the next change when W change from 1 to 2.

Hope that makes sense to you…

The condition is when W changes, so just run the coroutine (or use Invoke) then. So don’t check if W is a certain number, only run it when you do W++. However, you’re getting W from Math.Floor(S / 5). This is a great way to do it, but you don’t know when W changes implicitly. So, you need to store a “WTemp” variable to check when the real W changes (on every calculation of W, compare it with WTemp…if W != WTemp, run your function).

1 Like

ey! thanks…that´s a great idea…really thank you :slight_smile:

1 Like