Trying to destroy and object but it also destroys the parent. I have this script
public int life = 1;
int playerdamage = 1;
void OnCollisionEnter2D (Collision2D coll){
Debug.Log ("Something hit");
if (coll.gameObject.tag == "Ball") {
life = life - playerdamage;
Debug.Log ("Hit!");
if (life <= 0) {
Destroy (this.gameObject);
}
}
}
I have tested and know that the life variable is being changed in the parent when the child is hit. I am unsure why though.
@JoshuaMcKenzie I was thinking the same as I was reading, you know sometime you can be so set focused on something, then the more thought you put into it as your trying to finish may cause one to look too deep into a situation, having to take a step back or to let a fresh pair of eyes, ears/Brain is always useful. I find myself doing this at times (looking to deep into things and miss what’s in front of me.) I agree, Place the script on the child object!
@Kurt-Dekker They are all the same name. Should that matter since the collision is happening to a specific one. If that should matter, all the children should be destroyed but they are not. Only the one “hit”.
The problem is though that the parent also takes a hit from the children being hit.
Do Children colliders count for the parent? If that is the case this would make sense. Anyway to stop that if I am correct?
@JoshuaMcKenzie Ok, So I have it on the child and the parent. They should both take “damage”. Just separately. As of now the parent is taking damage when the child gets hit. (Though the child is not when just the parent is hit.)
I believe child colliders will effect their parents also. I don’t know for certain as I’d have to test it. I think it works this way because you might want to create multiple colliders on a target. But I think it’s more about how you have it written. The collider on the child checks if it’s hit by a ball and then does stuff. The parent may also be doing this.
Instead, the ball should do damage to the collider it hits. When the ball colliders, it should get the collider it hits and deal damage to it. Thus, that should be a single target.
I don’t know if that makes sense or not. But basically it’s the ball doing damage to a target vs the target taking damage from something hitting it (and then telling it’s parent about it).
I haven’t tested this, but should be easy for you to try out if this is the case or not. (and I could be wrong on this also)
Edit: To add, your ball will hit a target, check if it’s a valid target, getComponent on it to get the script it needs and probably call a method like deal damage (could even pass in a damage value if you want different damage amounts)
Is the parent object tag “Ball” if so then it will still take effect on both objects…
try to compare the name of the objects instead, to separate the “do something action” also you could always hard code and cache the exact game object and get component Collision2D you want things to happen to,
i.e …
public GameObject child;
public Collision2D c_child;
void start(){
c_child = child.getComponent<Collision2D >();
}
/// your check method
void OnCollisionEnter2D(Collision2D coll){
if (coll.gameObject.Collision2D == c_child ){
do something...
}
}