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 Maedhross · Oct 17, 2014 at 05:50 AM · triggernoob

OnTriggerEnter being called twice

So, I'm starting to play with Unity3D. I'm trying to make a double shot for my "Space Shooter" project.

Anyway, I've set a script for the bullets that have a function OnTriggerEnter. The bullet prefab is set to isTrigger and everything is working properly.

There's a litte problem though: The function makes a call to a function that updates the score. And, because the two bullets hits the object at the same time, it gets called twice.

I've searched for a solution and I couldn't find anything that worked. I could make a variable "numberOfHits" and make decreases on it, but I think it's not a good solution.

Here's my script:

 private var isCollision : boolean = false;
 function OnTriggerEnter(other : Collider){
     //Check for astroid
     if(other.gameObject.tag == "enemy" && !isCollision){
         isCollision = true;
 
         //Reset the position of the enemy
         other.GetComponent(scriptAstroid).ResetEnemy();
 
         //Create the explosion on impact
         if(explosion){
             Instantiate(explosion, transform.position, transform.rotation);
             audio.PlayClipAtPoint(fxSound, transform.position);
         }
 
         //Tell scene manager that we destroyed an enemy and add a point to the score
         sceneManager.transform.GetComponent(scriptSceneManager).AddScore();
 
         //Destroy the bullet
         Destroy(gameObject);
     }
 }


On the update function, I reset the isCollision variable to false again. But, because the two bullets hits the enemy AT THE SAME FRAME, it doesn't matter.

What do you guys suggest? Thanks in advance.

Comment
Add comment · Show 1
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 Andres-Fernandez · Oct 17, 2014 at 06:34 AM 0
Share

You can make the enemy call the AddScore function inside its reset function. If it only resets once, it will only add the score once. If it resets more than once, then you may need some sort of enemy manager that tracks the current active enemies.

1 Reply

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

Answer by g8minhquan · Oct 17, 2014 at 06:24 AM

If you want your Enemy only receiving 1 bullet hit, then put the isCollision bool check in the Enemy, not the bullet.

Comment
Add comment · Show 2 · 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 Maedhross · Oct 18, 2014 at 01:54 PM 0
Share

Ok, this worked man.

I have already changed the collision detection to the astroid. OnTriggerEnter is still called two times, but the function to AddScore is called just one time.

Thanks.

avatar image g8minhquan · Oct 19, 2014 at 04:05 PM 0
Share

You're welcome. If it solved your problem, please accept the answer

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making my Text fade in on a trigger 2 Answers

Spawn Point Problems 1 Answer

My if statements are not working [major noob] 2 Answers

Trigger Script Not Firing 4 Answers

Can't click gameobject when over another 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