- Home /
Instantiate: How to set the position of prefabs?
I want to instantiate a prefab. I have placed an EmptyObject in my scene. It is named HorseSpawn. I have attached a C# script to it named HorseSpawn. I have a "Horse" prefab in my Resources.
using UnityEngine;
using System.Collections;
public class HorseSpawn : MonoBehaviour {
// Use this for initialization
void Start () {
Instantiate(Resources.Load ("Horse") as GameObject);
}
// Update is called once per frame
void Update () {
}
}
I had assumed that it would inherit the position of the Empty to which the script was attached. It did not. So, what can I do to make it spawn at my EmptyObject?
I am very new to Unity (obviously) and just trying to get my head around the basics. Could you please point me in the right direction? The Unity docs are too complicated to understand. I think they assume we're all fluent in programming :)
Answer by Tehnique · Jul 21, 2014 at 02:09 PM
The instantiate documentation states that you can give the method a second and a third parameter. The second is the position you are looking for (for your case, set it as "transform.position"). The third is the rotation, you can jsut set it as Quaternion.identity.
Answer by Graham-Dunnett · Jul 21, 2014 at 02:08 PM
http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
The first form of the Instantiate
function lets you specify position and rotation.