- Home /
InvokeRepeating can't call method
Ok, i get this error when trying to use InvokeRepeating "Trying to Invoke method: Player.PosionDamage couldn't be called." What's wrong? I checked the reference and couldn't find anything different about their InvokeRepeating example.
void Update () {
if (poison == true){
Invoke("DartPoisonOff", PoisonTime);
InvokeRepeating ("PosionDamage", 0f, .5f);
}
void DartPoisonOff (){
poison = false;
}
void PoisonDamage(){
health -= PosionDamagePerHalfSecond;
}
}
invokerepeating has a ridiculous known bug where the first paramater cannot be zero. try 0.0001 and see if that helps.
DO NOT use invokerepeating in Update!! heh. it is very likely you want to use it in Start() r in some other function - not in update.
Lol thanks so much. That was 2/3 of my problems with this script. The other was a spelling error >.< I guess I have trouble typing Poison...
Answer by Fattie · Jul 24, 2012 at 09:20 AM
look, like this
say you want exciting poison effects to last for THREE seconds, and then end
function BeginPoisonEffectsNow
InvokeRepeating("_poisonOneStep", 0.001, 0.5);
Invoke( "_poisonCleanUp", 3.0 );
function _poisonOneStep
here, do your animation, health change or whatever
function _poisonCleanUp
CancelInvoke
that's it - it's that simple.
You see this everywhere constantly in video games. Some video game code is nothing more than zillions of these!
Answer by Kryptos · Jul 24, 2012 at 01:10 PM
Is is simply a spelling error: your method is called PoisonDamage but you are trying to call PosionDamage.
Anyway the other answers give some other useful hints about repeating actions.
Answer by pheash · Jul 24, 2012 at 09:06 AM
While poison is true it will call an InvokeRepeating on every cycle of the Update! you could use a counter that waits for poison time and then cancels the Invoke repeating. sorry i dont know C but this is what i think it should look like:
var poisonInstance : float; //this will hold the time when poison starts invoking. sorry dont know the syntax to declare variables in C.
void Update(){
if(poison == true){
poison = false; // this is now only called once!
poisonInstance = Time.time; // we set it to the current time
InvokeRepeating("PoisonDamage", 0f, .5f); // this command will call PoisonDamage() every .5 s until told otherwise by canceling it.
}
if(Time.time > poisonInstance + PoisonTime && IsInvoking("PoisonDamage")){ // here we check whether the current time is bigger than the time poison was true + PoisonTime (which i assume is holding how long the effect lasts for in seconds) and if we are invoking poison damage we cancel it.
CancelInvoke("PoisonDamage");
}
}
Hope that makes sence. let me know if you have any questions
pheash, you simply call CancelInvoke form another "clean up" routine you just invoke.
thanks fattie, that looks a lot neater. i am not very experienced in scripting and was looking forward to those sort of simplifications!
Yea, I made another variable to check and see if poison damage is already being dealt. And I have a clean up method that was also invoked. Thanks so much everyone! It's working perfectly now :)