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);
}
Answer by Oana · Feb 16, 2017 at 08:04 AM
You misspelled the function name :)
It's OnCollisionEnter2D, and not OnCollisonEnter2D
Oh my god, that's it! I can't believe it ended up just being a spelling error. I feel so dumb.
Thank you! :)
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.