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 /
  • Help Room /
avatar image
0
Question by jardinepatten · Feb 16, 2017 at 06:15 AM · c#oncollisionenterdestroy object

Cannot destroy object when it collides with another object?

I've looked through the help section and I cannot figure this out. I have a bullet prefab that does everything I need it to. It fires from my spaceship, it destroys objects and it will also destroy after a second if it hasn't hit anything.

I can't however, get it to destroy itself when it hits something else. I put the code to destroy an object in it's own script so I could reuse it when needed. The killObject script was added to the bullet prefab.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class killObject : MonoBehaviour {
 
     void OnCollisonEnter2D (Collision2D coll) {
         Destroy (gameObject);
         Debug.Log ("The bullet is hitting its target.");
     }
 }

It's incredibly frustrating that I can't do this. I've only just begun learning Unity and C#, and to not be able to do something that seems so simple is annoying me. Even the debug isn't doing anything.

The only thing I can think of is that a different script I have to determine if the player spaceship has lost its wings or it has been destroyed is affecting it. The main body of the ship and its two wings are children of the main parent. That code is in two parts, the code in the parent is below:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class parentScript : MonoBehaviour {
 
     public childScript body;
     public childScript leftWing;
     public childScript rightWing;
     public int wings = 2;
 
     public playerControl playerFS;
 
     public void RegisterCollision(childScript child, Collision2D c) {
         
         if (child == leftWing) {
             Destroy (child.gameObject);
             wings -= 1;
         } else if (child == rightWing) {
             Destroy (child.gameObject);
             wings -= 1;
         } else if (child == body) {
             Debug.Log (gameObject + " has been destroyed.");
             Destroy (gameObject);
         }
             
         if (wings == 1) {
             Debug.Log (gameObject + " has lost a wing.");
             playerFS.playerForwardSpeed = playerFS.playerForwardSpeed / 2;
         } else if (wings == 0) {
             Debug.Log (gameObject + " has lost both wings.");
             playerFS.playerForwardSpeed = 0f;
         }    
     }
 }

And the code in each of the children is as follows:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class childScript : MonoBehaviour {
 
     parentScript parent;
     void Awake() {
         parent = GetComponentInParent<parentScript>();
     }
 
     void OnCollisionEnter2D(Collision2D c) {
         parent.RegisterCollision(this, c);
     }
 
 }

All of that code works fine and does as intended. The ships can hit each other and they lose their wings and so forth if those colliders are hit. They also get destroyed if the bullet hits them. But still, the bullet doesn't get destroyed.

if there is something i've completely overlooked, then I hope someone can point it out to me. I'm completely stumped.

edit: I thought i'd quickly add the code where the bullet is Instantiated in case that helps:

 if (Input.GetKeyDown (FireKey)) {
     GameObject b = (GameObject)(Instantiate (bullet, transform.position + transform.up * 0.7f, Quaternion.identity));
 
     b.GetComponent<Rigidbody2D> ().AddForce (transform.up * 1000);
 
     Destroy (b, 1);
 }
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 hexagonius · Feb 16, 2017 at 07:17 AM 0
Share

how is it destroying other objects?

1 Reply

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

Answer by Oana · Feb 16, 2017 at 08:04 AM

You misspelled the function name :)

It's OnCollisionEnter2D, and not OnCollisonEnter2D

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 jardinepatten · Feb 16, 2017 at 08:09 AM 1
Share

Oh my god, that's it! I can't believe it ended up just being a spelling error. I feel so dumb.

Thank you! :)

avatar image Oana jardinepatten · Feb 18, 2017 at 06:59 AM 0
Share

I wasted so many hours of my life trying to figure out why something isn't working only to notice a spelling error with one of these kind of methods... it happens to everyone.

Visual Studio thankfully now has autocomplete for this kind of stuff, it's a bit harder with $$anonymous$$onoDevelop.

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

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

Unity3D: Objects destroy automatically 0 Answers

Unity 2D jump "fly" 0 Answers

2D Object Spawn Lag 1 Answer

C#: How can you make your character jump only when you are touching the top of the object? 1 Answer

Why i can't acces a global list in my class from OnDestroy() function 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