- Home /
Why is the method being called every frame even though it is InvokeRepeating?
This is a snippet of logic from a basic enemy I created. It is supposed to get the distance from the player, and if the player is close enough, Instantiate a Trigger box to deal damage, and then destroy the box. It is supposed to instantiate the box every 3 seconds, with the help of the InvokeRepeating. However, after the first initial 3 seconds, instead of continuing to get called every 3 seconds, it gets called every frame, resulting in insta-death. Everything else works fine though.
if (Vector2.Distance(player.transform.position, transform.position) <= 1.5f) {
InvokeRepeating("Attack",3f, //Works Fine
3f); //Gets ignored. Method gets called every frame.
} else {
CancelInvoke(); //Works Fine
}
Attack method:
void Attack () {
GameObject clone;
clone = Instantiate (attack, attackposition[i].position, Quaternion.identity) as GameObject;
Destroy(clone, .01f);
}
Ok, I got it not to call every frame, but the Ontriggerenter on the trigger won't call or do anything put inside of it.
There's no code here to even support that, so you should accept the answer as correct and start a new question after doing some research on triggers, there are many tutorials, videos, doc references, code snippets here on UA already (at the very least do some debug.log(other.name);
Guess I didn't do research! xD The solution was painfully simple.
$$anonymous$$aybe you're not using triggers at all if nothing's happening?
Answer by xortrox · Apr 22, 2014 at 10:17 PM
You are starting a new InvokeRepeating every frame as long as your if statement is true: if (Vector2.Distance(player.transform.position, transform.position)
If what I say is true then you need to check if you already started the invoke.