- Home /
What should I change in this instantiating a GameObject in a script using Unity?
I am trying to instantiate a GameObject using C# in Unity. It won't face the right direction or appear in the right position. Here is my script -
if(Input.GetKey("q")){ Instantiate(magicmissile, transform.position, transform.rotation); basicattackcooldown = 50; }
I believe my syntax concerning the transform.position and transform.rotation is incorrect. What should I change to correct it?
Answer by dannyskim · Dec 19, 2011 at 05:39 PM
When you call your Instantiate code and use transform.position and transform.rotation, you're using the transform information from the Game Object that the code is attached from, not the prefab that you're wishing to instantiate.
You need to provide the vector3 location that you want it to spawn at, and if you want no rotation on the prefab you need to use Quaternion.identity instead of transform.rotation.
Your answer
Follow this Question
Related Questions
C# cast object to gameobject not working 2 Answers
Player instantiates Backwords 0 Answers
How to put gameObjects to the list? 4 Answers