Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rizzutp · May 06, 2018 at 11:14 PM · oncollisionenterlivesgamestatehealth

Why OnCollisionEnter doesn't subtract properly player health

Hello everyone. I don't understand why everytime my player collides with an enemy, an amount of 5 lives is subtracted instead of 1 only!!!

This is the code of my OnCollisionEnter attached to the player:

     void OnCollisionEnter (Collision collision) {
 
         if (collision.gameObject.CompareTag ("Enemy")) {
             if (immortality == false) {
                 if (GameManagerScript.GetInstance ().GetLives () > 0) {
 
                     int tempCalc;
                     tempCalc = GameManagerScript.GetInstance ().GetLives () - 1;
                     GameManagerScript.GetInstance ().setLives (tempCalc);
                     immortality = true;
                 
                     if (immortality) {
                         StartCoroutine (Flasher ());
                         Invoke ("resetInvulnerability", 2);
 
                         if (GetComponent<MeshRenderer> ().material.color == Color.white || GetComponent<MeshRenderer> ().material == null) {
                             GetComponent<MeshRenderer> ().material = m;
                             GetComponent<MeshRenderer> ().material.color = c; 
                         }
                     }
 
                 } else if (GameManagerScript.GetInstance ().GetLives () == 0) {
                     dead = true;
                 }
             }
         }
             
     }

There are some methods not reported here: one is Flasher(), which is supposed to "flash" the player when is hit by an enemy giving some seconds of immortality, and the others are those contained within the GameManagerScript, which handle the player stats during the flow of the game.

Comment
Add comment · Show 3
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 Rizzutp · May 06, 2018 at 11:32 PM 0
Share

EDIT: I tried to manage the thing with the usage of a flag, as it is suggested in this post https://answers.unity.com/questions/738991/ontriggerenter-being-called-multiple-times-in-succ.html However, it doesn't work, and my player suffers a 5 lives subtraction ins$$anonymous$$d on 1 when an enemy hits him.

Probably the problem resides on the fact that my player has a sphere collider, but I don't really know how to manage it! :-(

avatar image Eno-Khaon Rizzutp · May 07, 2018 at 01:04 AM 1
Share

When OnCollisionEnter() is called, is it being called once at a time, or 5 times simultaneously? For example, if you put in a Debug.Log() just inside

 if (collision.gameObject.CompareTag ("Enemy")) {

does it show up 5 times simultaneously or just once?

Just to be sure, does it decrease the number of lives by 5, or does it decrease *to* a specific value? Just making sure nothing's being overlooked.

And finally, what do your GetLives() and SetLives() functions look like? Could the problem be in those, or even in Game$$anonymous$$anagerScript.GetInstance() ins$$anonymous$$d?

avatar image Rizzutp Eno-Khaon · May 07, 2018 at 09:20 AM 0
Share

I put some Debug.Log, it seems that OnCollisionEnter is called 5 times simultaneously, stopping at row 10 (so, the Flasher() coroutine is not processed).

Yeah: it decreases the number of lives by 5, not to a specific value.

Here are the two functions contained in my Game $$anonymous$$anager, GetLives() and setLives() respectively:

     public int GetLives () {
         return Game$$anonymous$$anagerScript.GetInstance ().lives;
     }

     public int setLives(int newLives) {
         Game$$anonymous$$anagerScript.GetInstance ().lives = newLives;
         return Game$$anonymous$$anagerScript.GetInstance ().lives;
     }

1 Reply

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

Answer by Rizzutp · May 07, 2018 at 09:39 AM

Ok, the error was that a multiple PlayerController component was attached to the player through the code!

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

83 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 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 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 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

how would I set up a regain health ui button 1 Answer

Lerpz Health Issue 1 Answer

How to destroy and respawn the player after loosing a life? 1 Answer

Character losing life event not firing 1 Answer

How to slowly decrease health script? 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