Hello,
I’m new to Unity (been using it for maybe a week or so) and a novice programmer (though I’d like to think I know the basics pretty well). I’ve been teaching myself Java through trial-and-error, the Unity forums/documentation, and reverse engineering through the scripts people have been generous enough to use/reference.
My problem: I have looked up Object.Destroy(). As I understand, this is the function that is used to despawn/kill an object from the scene. When I use it, all it seems to do is make the bullet stop in mid-air.
// variable declaration
var rocket : Rigidbody;
var speed = 100.0;
function Update ()
{
// firing function
TurretFire ();
}
function TurretFire ()
{
// when user pushes the spacebar
if (Input.GetKeyDown ("space"))
{
// confirm that the spacebar has been pressed
print ("Space is pressed.");
// instances the rocket mesh
var rocketClone : Rigidbody = Instantiate(rocket, transform.position, transform.rotation);
// gives the rocket mesh a velocity and fires
rocketClone.velocity = transform.forward * speed * -1;
// destroys the rocket after 1 second
Destroy (rocketClone, 1.0);
}
}
I theorize it just deletes the Rididbody thus getting rid of the physics aspect of the bullet. Therefore, I tried
Destory (GameObject, 1.0); or Destroy(this, 1.0);
in place of
Destroy (rocketClone, 1.0);
and it takes away my ability to fire rockets all together as well as failing to remove the prefab rocket from the scene.
I’m confident it’s just a small thing I’m missing, but I can’t seem to figure it out. Any thoughts on the matter will be helpful.
Thanks,
-S