- Home /
Question by
RandomUser123 · Sep 20, 2014 at 09:09 PM ·
instantiatebuttonclone
Having an issue with instantiating objects on button clicks
Theres a button in my scene and I want to instantiate an object when I press it but it returns the error:
Type IPointerClickHandler expected Button received. i:0. The variable bullet of helicopter has not been assigned.
You probably need to assign the missile variable of the helicopter script in the inspector
public Rigidbody bullet;
public void fire()
{
Rigidbody clone;
clone = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
}
I have the bullet prefab on my object in the inspector and my button uses the function above when clicked so I don't know what the problem is, if anyone could help, I'd appreciate it
Comment
Best Answer
Answer by Louis Watson · Sep 20, 2014 at 10:04 PM
change the code to:
public GameObject bullet;
public void fire()
{
GameObject clone;
clone = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
clone.rigidbody.velocity = transform.TransformDirection(Vector3.forward * 10);
}
you may need to delete and recreate the button in the scene for some reason to get it to work.
That's gotten rid of the error but it doesn't actually Instantiate anything :/
@Louis Watson, strange error that, worked when i added another button like you said, anyway thanks a million for the help