Collision not working (160868)

Hi!
I dont know why my Collision is not working, If the shot hits the target ,the target should be destroyed. I have added the script to the target.

using UnityEngine;
using System.Collections;

public class destroygegner : MonoBehaviour {

	public GameObject shot;

	void OnCollisionEnter (Collider other) {
		if (other == shot) {
			Destroy (gameObject);
			}
		}
	}

1 Answer

1

Try this:

void OnCollisionEnter (Collision other)

error CS0019: Operator '==' cannot be applied to operands of type 'UnityEngine.Collision' and 'UnityEngine.GameObject'

–

Try this: if(other.name == "Shot") or if (other.gameObject.tag == "shot")

–

SORRY! I don't know why I'm having so much trouble on giving you the right answer. This should work void OnCollisionEnter (Collision Colider) { if (Colider.gameObject.name == "Shot") { Debug.Log ("hit"); Destroy (gameObject); } } } Name your shot "'Shot"

–

Why is you enemy's collider a trigger? It is not going to know what hit it unless you use OnTriggerEnter.

–

Make sure all Colliders are NOT triggers and change you script to this: void OnCollisionEnter (Collision other) { if (this.gameObject.name == "Shot(Clone)") { Destroy (other.gameObject); } } } When your shot is instantiated it will automatically name the Shot with a (clone) at the end. So you're Shot will be Named Shot(Clone). A better way to do what you're trying to do would be to find the target name and destroy it.

–