Play method only ONCE when detected by the update function

It seems like there should be a pretty simple solution to this problem I’m having but I can not figure it out no matter how hard I try. I’m just trying to make it so that once the update method gets to the “applykillforce();” method (in line 41), it only runs it once, and never gets played again. Right now it runs it over and over again, which is not what I want (because it makes the enemy spaz out).

If anyone has a solution, it would be greatly appreciated, as I have been stuck on this for awhile.

BTW I am new to coding so if you guys could be specific and try to keep it simple for me that would also be great.

Ok, first off your using a Update method, which it’s going to be called every frame if Peeker is enabled. Second you should use a tick method;

void Test()
{
bool ticked; 
if(ticked && Peeker.GetComponent<PeekerDie().enabled)
{
ticked = false
// Run code here
}
else if(!ticked && !Peeker.GetComponent<PeekerDie().enabled)
{
ticked = true
}  
}

If this need revisions, or this doesnt work, just comment.

Note: This can be runned multiple times, if you dont want that remove the else if statement, and set ticked to true. Also, this can be used in an update method, but the ticked will keep switching to true and false, if peeker is also enabled or disabled, you can run void Test on Update.

// has the method been run already?
public bool hasAppliedKillForce;

void Update()
{
    if (Peeker.GetComponent<PeekerDie>().enabled == true && hasAppliedKillForce == false)
    {
        // use a coroutine
        StartCoroutine(applyKillForce());
    }
}

IEnumerator applyKillForce()
{
    // do something
    // ensure coroutine can not be triggered again
    hasAppliedKillForce = true;
    yield return null;
}

int killForceCount = 0;

Update() { If(killForceCount == 0 && your condintion )
{
killForceCount++;
ApplyKillForce();
}
}

Just add a counter variable and add one to it each time you call the function. If you only want to call it 3 times for example then you would say if count is less than 3 call the function. Otherwise don’t. :slight_smile: sorry for the sloppy code writing on my phone.