[SOLVED]Destroy prefab

Updated on December 21, 2015 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on December 21, 2015

Hey everyone,

I am trying to destroy my prefab with the “Destroy” component, but the only thing happening to my bullets/beams is the fact that they stop moving, however they do not fully disappear from the scene. From what I understood it has to do with the fact that “Beam” in my script is a RigidBody2D and Destroy applies to Objects, but I am still trying to understand their difference..

using UnityEngine;
using System.Collections;

public class Shooting : MonoBehaviour {

public Rigidbody2D Bullet;
public int MoveSpeed;

void Update () {
if (Input.GetKey (KeyCode.Mouse0)) {
Rigidbody2D Beam;
Beam = Instantiate(Bullet, transform.position, transform.rotation) as Rigidbody2D;
Beam.AddForce(-transform.right * 200);
Destroy(Beam,1);
}
}
}

  • Liked by
Reply
2 on December 21, 2015

I think I found the solution: I am attempting to destroy the rigidbody component of my object, but i should destroy the whole object, thus Beam.gameObject should do the job (?). It is working apparently

on December 21, 2015

Yeah, that’s how you do it.

Helpful
on December 21, 2015

Thx !

Show more replies
  • Liked by
Reply
Cancel