user3482470
user3482470

Reputation: 37

Unity 3d how to instantiate object in a certian position

using UnityEngine; using System.Collections;

public class playSound : MonoBehaviour {

private bool destructionHasBegun = false;
public Transform BlueKey;


private void OnTriggerEnter()
{
    audio.Play ();
    destructionHasBegun = true;
}

private void Update()
{
    if(destructionHasBegun)
    {
        DestroyWhenSoundComplete();
    }
}

private void DestroyWhenSoundComplete()
{
    if(!audio.isPlaying)
    {
        Destroy(gameObject);
        GameObject textObject = (GameObject)Instantiate(Resources.Load("BlueKey")); 





    }
}

}

im trying to instantiate the bluekey prefab in a particular position how do i do this? thanks in advance

Upvotes: 2

Views: 3772

Answers (1)

Chris McFarland
Chris McFarland

Reputation: 6169

As per the Instantiate documentation, you can pass in a Vector3 worldspace position as a second parameter.

You'll also need to pass in a Quaternion for its initial rotation as the third parameter; Quaternion.identity is the equivalent of no rotation.

Vector3 newPosition = new Vector3(0, 0, 0);
Quaternion newRotation = Quaternion.identity;
GameObject textObject = (GameObject)Instantiate(Resources.Load("BlueKey"), newPosition, newRotation);

Upvotes: 0

Related Questions