• 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 OnEd0t · Jul 21, 2019 at 08:45 PM · ontriggerenterontriggerexit

OnTriggerEnter/Exit are activating twice

Hi, I am working on a simple FPS Game and the enemies dont shoot instead they try to touch you, and if they do you lose a life, pretty simple stuff, i do it using OnTriggerEnter to check if the player collides with something, but onTriggerEnter activates twice and this causes the player to lose two lives, it is not consistant sometime the player loses one life and other times he loses two, tried OnTriggerExit but still the same problem, if there is a way to work around it or a better way to do it, would appreciate any help.

Here is the code:

public class Collission : MonoBehaviour

{

 public GameObject player;
 public GameManager gameManager;
 public Text livesText;

 public int lives = 5;

 private Vector3 ZeroPoint = new Vector3(0, 37, 0);

 void OnTriggerEnter(Collider collisionInfo)
 {
     if (collisionInfo.GetComponent<Collider>().tag == "Enemy")
     {
         transform.position = ZeroPoint;
         lives--;
     }
 }

 void Update()
 {
     Debug.Log(lives);

     livesText.text = lives.ToString();

     if (lives <= 0)
     {
         //Finds Another Script And activates the EndGame function
         //which Reloads The Scene.
         FindObjectOfType<GameManager>().EndGame();
     }
 }   

}

Comment
Add comment · Show 2
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 Casiell · Jul 22, 2019 at 06:49 AM 0
Share

Are you sure each enemy has only one collider? Each collider will give you OnTriggerEnter message even if they are under the same Rigidbody

avatar image OnEd0t Casiell · Jul 22, 2019 at 02:15 PM 0
Share

yes sur i am sure.

1 Reply

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

Answer by OnEd0t · Jul 22, 2019 at 02:37 PM

Um..Okay..this is kinda embarassing..turns out that the first pirson controller script is a collider in itself and i had both the first person controller script and the capsule collider on the player gameobject. sooooo... yeah thanks for the note.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Trigger on MeshCollider Only Seems To Work at Edges of Mesh 1 Answer

OnTriggerEnter/OnTriggerExit called multiple times with MeshColliders 2 Answers

Have "OnTriggerStay" detection but only call it once? 1 Answer

OnTriggerEnter/Exit? 1 Answer

OnTriggerEnter/Exit not called sometimes 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