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
0
Question by Overflo · Sep 29, 2012 at 05:21 AM · c#triggercoroutineontriggerstay

Want health to decrement longer stays on trigger

When OnTriggerEnter event is fired then health decrements by 5. Now I want it to check every so many seconds and if still there then decrement another 5.

I had absolutely no success with OnTriggerStay so OnTriggerEnter sets a bool to true and OnTriggerExit sets to false.

What I want is while true it will decrement but obviously not on Update.

I have tried calling a coroutine with a yield in which waits the first time, and then decrements on every update so I am obviously not calling it correctly

The Coroutine is simply this

 IEnumerator OnSpikes()
 {    
      
      audio.PlayOneShot(spikesSound);
      yield return new WaitForSeconds(5.0f);
      health -=1;
 }

I have tried calling the coroutine from all over the place (can't even remember them all) and in while loops Anyway, can someone lead me in the right direction (I have searched and searched and tried all sorts of things but I'm obviously missing something)

thanks in advance }

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
1
Best Answer

Answer by shaderop · Sep 29, 2012 at 07:28 AM

Try something like this:

 private bool isInTrigger = false;

 void Start()
 {
     StartCoroutine(OnSpikes());
 }   

 void OnTriggerEnter(Collider other)
 {
     isInTrigger = true;
 }

 void OnTriggerExit(Collider other)
 {
     isInTrigger = false;
 }

 IEnumerator OnSpikes()
 {    
     while (true)
     {
         if (isInTrigger)
         {
             audio.PlayOneShot(spikesSound);
             health -=1;     
         }
         
         yield return new WaitForSeconds(5.0f);          
     }
 }
Comment
Add comment · Show 4 · 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 kmeboe · Sep 29, 2012 at 07:43 AM 1
Share

This answer should work too -- the one downside is, since it's running all the time, you'll get different waits for the first run-through after each trigger. e.g. if you're 2 seconds through "WaitForSeconds" when the bool is turned on, your code will wait 3 seconds to decrement the health.

avatar image Overflo · Sep 29, 2012 at 07:51 AM 0
Share

Thankyou - works perfectly -

avatar image Overflo · Sep 29, 2012 at 07:55 AM 0
Share

I see what you mean kmeboe. For this instance it will be okay, but will definitely look into your solution. For now will stick with this as the assignment is due $$anonymous$$onday and it is extra functionality that I have added in, Thanks again to you both

avatar image shaderop · Sep 29, 2012 at 07:56 AM 0
Share

@Overflo You're very welcome. But as kmeboe said, the coroutine will be running all the time, but I don't think that should cause any noticeable performance issues since it's only invoked every few seconds and the code is very $$anonymous$$imal.

avatar image
1

Answer by kmeboe · Sep 29, 2012 at 07:24 AM

Greetings,

If you call StartCoroutine before the previous one has ended, this error will occur. My advice is to set a boolean to "true" in OnTriggerEnter (or wherever), then during Update() launch your Coroutine if the boolean is "true". The end of the Coroutine will then set this boolean to "false".

See the answer here for an example: http://answers.unity3d.com/questions/240794/coroutine-for-reloading.html

-Kevin

Comment
Add comment · 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

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

11 People are following this question.

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

Related Questions

Work around for onTriggerStay? 1 Answer

Multiple Cars not working 1 Answer

How to run a coroutine if in a trigger but run a different one if outside it? 1 Answer

Call function on all objects in trigger 2 Answers

Distribute terrain in zones 3 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