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.

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.

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.