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 Xeus · Dec 09, 2014 at 08:48 PM · collisioncollision detectioncollision2d

OnCollisionEnter2D doesn't work every time.

I'm making a asteroids game and every time i shoot a asteroid, i want to gain 10 or 20 points. The way i set it up on the asteroid is:

 void OnCollisionEnter2D(Collision2D col)
     {
         if (col.gameObject.tag == "Shot")
         {
             Destroy(col.gameObject);
             Destroy(gameObject);
             if (childAsteroid != null)
             {
                 for (int i = 0; i < spawnAmount; i++)
                 {
                     Instantiate(childAsteroid, transform.position, transform.rotation);
                 }
             }
         }
     }

On the bullet:

 void OnCollisionEnter2D(Collision2D col)
     {
         if (col.gameObject.tag == "smallAsteroid")
         {
             Gamemanager.points += 10;
         }
         else if (col.gameObject.tag == "mediumAsteroid")
         {
             Gamemanager.points += 20;
         }
     }

And in the gamemanager:

 public class Gamemanager : MonoBehaviour
 {
     public static int points;
     public Text text;
 
     // Use this for initialization
     private void Start()
     {
     }
 
     // Update is called once per frame
     public void Update()
     {
         text.text = "Points: " + points;
     }
 }

Sometimes i gain points and sometimes i don't... I can't figure out why.

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

1 Reply

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

Answer by MrSoad · Dec 08, 2014 at 09:12 PM

You are Destroying the bullet ("Shot") before it has run its collision code. One collision will always be processed before another, sometimes the Bullet collision is run first(and it works how you want) but sometimes the Asteroid collision is run first and this Destroys your Bullet so the Bullet collision script never runs.

To fix this add your score in the Asteroid collision script, and put your Destroy code at the very end of this script. Get rid of the code in your Bullet collision script that relates to the Asteroids (only!) completely.

If you are using the same script on all your asteroids then add a public Bool var Is_Medium to the script and set this up in on the script in the inspector window for each of the two prefabs. Now use this Bool to determine what score to add when this event occurs.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Object passes through collider even when isTrigger is turned off. 2 Answers

Collision point problem 1 Answer

What does it mean to be "grounded"? 0 Answers

Why can't I destroy this object!? 3 Answers

Best way to manually calculate collision between GameObjects and a Tilemap without using physics 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