Collision not working
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);
}
}
}
Answer by Zoogyburger · Mar 13, 2016 at 08:22 PM
Try this:
void OnCollisionEnter (Collision other)
Try this:
if(other.name == "Shot")
or
if (other.gameObject.tag == "shot")
Now it just runs without doing anything, it just accepted the code but doesnt do what the code is for xD
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"
still doenst work :/ okay here is how I created the shot: I made a shot prefab, added a script for moving forward, it got a Rigidbody, its Box Collider is trigger and gave the player a script for shooting it on keypress. The enemy is a sphere with a nav mesh agent for moving torwards the player and got a sphere Collider wich is a trigger. Also the console is completely empty even when they should hit. What did I do wrong?
Why is you enemy's collider a trigger? It is not going to know what hit it unless you use OnTriggerEnter.
It still doenst work, I dont know what I do wrong ... ._.
$$anonymous$$ake 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.
Now that both Colliders are no triggers anymore it works, thank you for your time and your help! :) But I changed your script a bit and added it to the shot :) I still have to learn a lot :D
void OnCollisionEnter (Collision other)
{
if (other.gameObject.name == "Gegner") {
Debug.Log ("hit");
Destroy (gameObject);
Destroy (other.gameObject);
}
Destroy (gameObject);
}
Good! That script should work much better, but you don't need the last destroy line.
Your answer
![](https://koobas.hobune.stream/wayback/20220612071707im_/https://answers.unity.com/themes/thub/images/avi.jpg)