- Home /
Having problems with Instantiate.
So, I use handlers for everything so I need to find a way to spawn an arrow and project it forward. I have a gameObject named Arrow with the model attatched to it.
I have AttackHandler attatched to my Player and want it to fire an arrow (Which is another game object) when I hit space, could someone please show me how to use instantiate properly to clone Another game object, rather than cloning the one the script is attatched to? Thank you. The reason I'm doing it this way is I want AttackHandler to handle shooting fireballs, arrows, and other projectiles.
Please don't link me to the script reference, I already have it up. ..
EDIT: Still not getting it
Answer by cdrandin · Jan 14, 2013 at 05:18 AM
var projectile:GameObject
Instantiate(projectile, Vector3(0, 0, 0), Quaternion.identity)
The thing is have your handle be attached to a spawn location, which is an empty game object. So you can just reference its location like transform.position.
so.. Instantiate(projectile, transform.position, Quaternion.identity)
Answer by Hivemind · Jan 14, 2013 at 05:25 AM
This is how it would be done in C#.
GameObject arrowPrefab; // be sure to assign it
void SpawnArrow()
{
//Spawns arrow a ahead from player's position and uses the player's orientation for the arrow's orientation.
GameObject newArrow = Instantiate(arrowPrefab, transform.position + transform.forward * 2, transform.rotation) as GameObject;
}
This code doesn't work
The name Vector' does not exist in the current context The best overloaded method match for
UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments rgument #2' cannot convert
object' expression to type `UnityEngine.Vector3'
I misspelled the Vector class. I changed it to transform.forward because it will be local to that object. Anyways, the second parameter is its position. We take the objects current position, face forward (transform.forward), then move two units in that direction so it will appear in front.
Your answer
