- Home /
Destroy method returning null.
Okay, so my fire script is this;
var projectile : Transform;
var bulletSpeed : int = 1000;
function Update ()
{
var fireBall = GameObject.Find("spawnPoint");
// Put this in your update function
if (Input.GetMouseButtonDown(0)) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Transform;
clone = Instantiate(projectile, fireBall.transform.position, transform.rotation);
// Add force to the cloned object in the object's forward direction
clone.rigidbody.AddForce(clone.transform.forward * bulletSpeed);
}
Destroy(clone.gameObject, 5);
}
This actually instantiates a clone, however the line
Destroy(clone.gameObject, 5);
Returns object cannot be set to an instance? I have no idea why it returns that. I've even referenced it to clone.gameObject as it couldn't convert unity.engine transform into w/e.
Any suggestions?
Answer by Kleptomaniac · Apr 10, 2012 at 04:22 PM
I believe that's to do with the fact that you are destroying clone.gameObject outside of your Input.GetMouseButtonDown, in which your clones are actually instantiated, meaning that you're trying to destroy something that doesn't exist or is null and you're getting that error. Chuck your destroy line within the if statement and you should be right.
Hope that helps, Klep
This did solve it. Thanks tho, can't believe I'm making little mistakes :/
Heh, so true. Right, back to making more baby mistakes :D