- Home /
Question by
pizzaninja467 · Apr 17, 2018 at 03:08 PM ·
c#destroydestroy object
Hi Im just trying to get my projectile to destroy a game object and Im having a hard time figuring it out. Any help is appreciated.c# :)
Heres the projectile script if it helps:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class laser : MonoBehaviour { public GameObject laserPrefab;
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Fire2"))
{
GameObject nb = (GameObject) Instantiate (laserPrefab, this.transform.position, this.transform.rotation);
nb.GetComponent<Rigidbody> ().AddForce (this.transform.forward * 1000);
}
}
}
Comment
Answer by dbarnsdal · Apr 17, 2018 at 03:32 PM
So, I've done this by adding a tag to the laser prefab and then in the script of the object you want destroyed, using the following code.
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "laser")
{
Destroy(this.gameObject);
}
Awesome thank you. Do i need to do anything in the unity editor?
Yes, in the inspector of the prefab, you'll have to add a tag.
Your answer
Follow this Question
Related Questions
Destroying a Button when you click it. 1 Answer
Multiple Cars not working 1 Answer
how to destroy enemy 1 Answer
Distribute terrain in zones 3 Answers
Enemy Spawner help 1 Answer