Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Danao · Jul 04, 2015 at 01:30 AM · collidertriggerdeathlives

Yield Continuous Death

Hello all, I'm trying to get my brain back into Unity and C# after a hiatus and could use your help! When my player falls to his death and collides with a box collider, this IEnumerator is triggered. The only problem is that livesLost is updating to remove more than one life (as if it is being triggered continuously). I thought the 1 second yield would prevent that, but I guess I'm thinking incorrectly. Any help on how to fix this code to assure my player only loses one life per plummet would be greatly appreciated!

    private IEnumerator DieAnim ()
     {
             CoinScript livesLost = this.GetComponent<CoinScript> ();
             livesLost.UpdateLives (1);
             iAmDying = true;
             humanDeath.Play ();//plays the death sound
             animator.SetInteger ("AnimState", 1);
             yield return new WaitForSeconds (1);//Use the length of the animation clip as the wait time for yield
             Application.LoadLevel (Application.loadedLevel);//reload the current level
             }
Comment
Add comment · Show 4
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 starikcetin · Jul 04, 2015 at 02:09 AM 0
Share

This part of script does waht you said exactly, maybe the code that triggers this part of code has problems, post that part of script too.

avatar image Danao · Jul 04, 2015 at 02:51 AM 0
Share

Thanks for the input. Here's the part of the code that triggers the IEnumerator:

     } else if//if the player collides with destroyer
                             (collision.gameObject.tag == "Destroyer") {
                             Platformer2DUserControl stopmove = GetComponent<Platformer2DUserControl> ();
                             Destroy (stopmove);
                             StartCoroutine ("DieAnim");
 }


avatar image starikcetin · Jul 04, 2015 at 02:55 AM 0
Share

Is that code in Update or OnTriggerStay?

avatar image Danao · Jul 04, 2015 at 03:55 AM 0
Share

OnCollisionEnter2D. Perhaps that's part of my problem?

1 Reply

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

Answer by Ibzy · Jul 04, 2015 at 11:39 PM

Sounds to me like the event is being triggered continuously when he hits the deck (constant collision).

I'd look to adding an isDead bool and checking for that in your if statement. I'd set it to true at the start of the coroutine and then false after Application.LoadLevel().

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 Danao · Jul 05, 2015 at 06:47 AM 0
Share

Hi, Ibzy and thanks for the input. With your suggestion along with changing the way I'm updating lives it seems to be working...now on to triggering the "Game Over" screen correctly when my player's lives dip below zero :D

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't click gameobject when over another trigger? 1 Answer

OnTriggerEnter fails to activate 1 Answer

OnTriggerEnter firing twice 1 Answer

Enemy line of sight using linecast and colliders 1 Answer

Rigidbody Trigger 1 Answer


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