I’m trying to destroy an object called “Spear” after it collides with the “ground”. But it just keeps going?
using UnityEngine;
using System.Collections;
public class LauncherScript: MonoBehaviour {
void Update(){
transform.Translate(Vector3.right*30 * Time.deltaTime, Space.World);
}
void OnCollisionEnter(Collision other) {
if(other.collider.name == "ground")
{
Debug.Log("Collided");
Destroy(other.gameObject);
}
}
}
I didn’t have a rigidbody on the spear. Plus the script was wrong.
I put
public class LauncherScript: MonoBehaviour {
void OnCollisionEnter(Collision other) {
if(other.collider.name == "Spear")
{
Debug.Log("Collided");
Destroy(other.gameObject);
}
}
}
on the ground and
public class SpearLauncher : MonoBehaviour {
void Update(){
transform.Translate(Vector3.right*30 * Time.deltaTime, Space.World);
}
}
on the spear
Solokeh
2
Try putting this in your “ground” object:
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "spear")
{
Destroy(col.gameObject);
}
}
}
You should be doing
if (col.GameObject.name == "Name Here")
{
Destroy (GameObject.find ("Other Name Here");
}