Destroying GameObject on collision Problems (Begginer)
The script below is a script for shooting in a 2d top down game, I've tested the shooting/raycast and they work. I just don't know how to destroy the object that is shot.
using UnityEngine;
using System.Collections;
public class Weapon : MonoBehaviour {
public float fireRate = 0;
public LayerMask whatToHit;
public float Range = 0;
public EnemyHealth _EnemyHealth;
public GameObject Body;
float timeToFire = 0;
Transform firePoint;
// Use this for initialization
void Awake () {
firePoint = transform.FindChild ("firePoint");
}
// Update is called once per frame
void Update () {
if (fireRate == 0) {
if(Input.GetKey(KeyCode.Space)){
Shoot ();
}
}
}
void Shoot () {
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, Vector2.up, Range, whatToHit);
if (hit.collider != null) {
Destroy(hit.collider);
}
}
}
Comment
Best Answer
Answer by Statement · Oct 09, 2015 at 11:48 PM
Destroy(hit.collider);
This will only destroy the Collider you hit with your Raycast.
If you want to destroy the GameObject that the Collider is attached to, try this:
Destroy(hit.collider.gameObject);
Your answer
Follow this Question
Related Questions
Adding points to Collider as element of a list. 0 Answers
Objects wont stop bouncing if they fall. 0 Answers
Oject not detecting when being shot at 0 Answers
Check for collision ONCE 0 Answers
Make a player respawn when colliding with a certian object? 0 Answers