Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by fireomega · Feb 15, 2012 at 07:32 PM · functionbulletoffregeneratehealth

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adamonline45 · Feb 15, 2012 at 08:20 PM 0
Share

Ah, Berenger, you concisely summed up my answer that is pending approval :P

avatar image fireomega · Feb 15, 2012 at 08:24 PM 1
Share

What would you write in script form though?

avatar image Berenger · Feb 15, 2012 at 08:56 PM 0
Share

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

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fireomega · Feb 15, 2012 at 10:00 PM 1
Share

Thanks for your efforts but i'm too noobish at unity to be able to make it properly work in my script

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Health and Damage [C#] 2 Answers

So I am trying to make my character regenerate health but reset the time it takes to regen when they are hit. I'm lost and have been at it for hours thanks in advance 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges