- Home /
This question was
closed Sep 22, 2013 at 06:53 AM by
Fattie for the following reason:
Duplicate Question
Question by
jatmdm · Sep 22, 2013 at 06:18 AM ·
javascriptinstantiateprefabfunction
Calling a function from a PreFab
I'm trying to get a bullet to go in the direction of a gun in 2 dimensions, but I'm finding it hard to pass in the variable to the prefab.
if(Input.GetKeyDown(KeyCode.Space))
{
iBullet = Instantiate(bullet, transform.position, Quaternion.identity);
iBullet.setDirection(gunRotation);
}
The Prefab, bullet, has a script that has the setDirection function within it. How do I call this?
Comment
Best Answer
Answer by aldonaletto · Sep 22, 2013 at 06:25 AM
You must get the script with iBullet.GetComponent. A script becomes a custom component after compiled, and its type is the file name without any extension. Supposing that this script is BulletScript.js, its type is BulletScript - and you can do what you want this way:
...
iBullet.GetComponent(BulletScript).setDirection(gunRotation);