- Home /
Destroying a prefab with a prefab?
Hi guys,
I've been on google all night and I've just been getting more and more confused so thought I'd ask for some help.
Basically I have a game object with this script:
public var secondsToDestroy = 5;
private var speed = 500;
var shotDelay = .5;
//var instantiatedProjectile : Rigidbody;
var projectile : Rigidbody;
function Start() {
while (true) {
while (!Input.GetKey("space")) yield;
// Fire bullet here
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );
yield WaitForSeconds(shotDelay);
Destroy (instantiatedProjectile.gameObject, secondsToDestroy);
}
}
and this essentially launches a bullet prefab that's been attached to the script.
Now I've been trying to set up collision using raycasts which is going okay...
So I have this script attached to the bullets:
function Update () {
var forward = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(transform.position, forward * 10, Color.green);
if(Physics.Raycast(transform.position, forward, hit, 10)){
Debug.Log("Hit");
if(hit.collider.gameObject.name == "Asteroid(Clone)"){
Debug.Log("asteroid destroy!");
//Code to destroy bullet
//Code to destroy asteroid
//Destroy (this);// not working :(
}
}
}
So that's all the shooting code really. I've done a lot of research but nothing too clear and a lot is really confusing as I'm new to unity so if someone could point me in the right direction I'd be really grateful.
just everything in enter to destroy the bullet or asteroid doesn't work. I'm not sure if it's because it's a prefab and I have to somehow reference the specific clone to destroy it or something else. I was thinking that maybe I should make the asteroid and bullet global values so I could access them from other script but I wasn't too sure how tbh...
Also here's the Asteroid script if it helps at all.
var thePrefab : GameObject;
var asteroidCount = 0;
function Start () {
for( i = 0; i < asteroidCount; ++i){
var instance : GameObject = Instantiate(thePrefab, Vector3(Random.Range(500,1500), Random.Range(-100, 200), Random.Range(500, 3500)), Random.rotation);
instance.transform.localScale = Vector3.one * Random.Range(8,20);
}
Thanks again guys. Really trying to figure this out on my own but I guess every now and again you need a point in the right direction...
Still can't figure this out... I can't see what I'm doing wrong :S
$$anonymous$$gestions anyone??
Answer by Meltdown · Mar 09, 2012 at 02:41 PM
Use the instance variable from this line to destroy your instantiated object...
var instance : GameObject = Instantiate(thePrefab, Vector3(Random.Range(500,1500), Random.Range(-100, 200), Random.Range(500, 3500)), Random.rotation);
Destroy(instance);
So somehow you want to put your instantiated object into a GameObject variable. Then you just destroy that instance.
Answer by Bunny83 · Mar 09, 2012 at 03:21 PM
this referes to the script, not the the GameObject the script is attached to. To destroy the object you collided with use:
Destroy(hit.collider.gameObject);
To destroy the bullet-gameobject use:
Destroy(gameObject);
gameObject is a property of your script which returns the GameObject this script is attached to. Destroy(this) will only destroy your bullet script.
Your answer
Follow this Question
Related Questions
Destroy Turret with machine Gun 0 Answers
Destroy a GameObject 2 Answers
RayCast Collision between 2 game objects (of the same prefab) 1 Answer
Collision Detection for a Prefab? 3 Answers
ray destroying prefab not clone? 2 Answers