- Home /
Turn off function
Is it possible to turn off a function within the script. I've made a health script with a regeneration function like out of call of duty. Is it possible to make it that the function doesn't work if e.g. your hit by a bullet.
Answer by Berenger · Feb 15, 2012 at 08:07 PM
To control a single function, it's usually with a boolean if(...) return;
To control an entire script, Components have an enabled property just for that.
Ah, Berenger, you concisely summed up my answer that is pending approval :P
If we don't write you a lot of code, there is a reason. The more you do by yourself, the more you will remember. That's not difficult, try to understand the logic we gave you and test yourself. $$anonymous$$ake mistakes, correct and celebrate !
@adamonline45 UnityAnswer is an unfair world :p
Answer by adamonline45 · Feb 15, 2012 at 08:37 PM
One way to achieve this would be to have a 'flag' that indicates whether or not the player is regenerating health. Normally flags come in the form of boolean values. When a player gets shot, you can set 'isRegenerating' to false. Then in your regeneration function, you can put at the top:
if ( ! isRegenerating )
return;
This will cause the function to abort without executing the regeneration code.
If you wish to re-enable regeneration after some amount of time, look into coroutines, and create one that sets isRegenerating back to true after a delay. Reset the delay if the player gets shot again.
Another approach would be to create a new monobehaviour that is just for regenerating health. If the regeneration code is in the Update method, you can control whether or not the health regenerates via the component's 'enabled' property. When that is false, the update method will not execute, and the player will not gain health.
The bottom line is that you can't enable or disable functions, per-say, but you can control whether or not they get called.
Thanks for your efforts but i'm too noobish at unity to be able to make it properly work in my script
Your answer
Follow this Question
Related Questions
Health and Damage [C#] 2 Answers
My bullet script doens't work. 1 Answer
Sidescroller health bar collision script with regen? 1 Answer
Player Health drops fast, how to pause after damage 0 Answers