- Home /
How to decide an object's position when using Instantiate?
Hi, I'm currently doing a Udemy tutorial where I'm making a 2D game in Unity. In the game there is a script that checks to see if there's a "player" object in the scene and if there is not then one is spawned in. Here's the script for that:
public class EssentialsLoader : MonoBehaviour
{
public GameObject UIScreen;
public GameObject player;
public GameObject gameManager;
// Start is called before the first frame update
void Start()
{
if(UIFade.instance == null)
{
UIFade.instance = Instantiate(UIScreen).GetComponent<UIFade>();
}
if(PlayerController.instance == null)
{
PlayerController clone = Instantiate(player).GetComponent<PlayerController>();
PlayerController.instance = clone;
}
if(GameManager.instance == null)
{
Instantiate(gameManager);
}
}
// Update is called once per frame
void Update()
{
}
}
When I instantiate the "player" GameObject, how do I decide where it spawns? I tried moving the parent object in the scene, adding the transform as an argument, and adding a line that says "player.transform.position = new Vector 3 . . . ", but none of this worked.
Answer by SauronDark · Apr 08, 2019 at 04:43 AM
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Instantiate also takes 3 parameters as first one Gameobject, then its position and rotation
Instantiate(Object original, Vector3 position, Quaternion rotation);
I tried that, it didn't work; I created a new Vector 3 at a position I wanted and a Quaternion called rotation and I did Instantiate(player, position, rotation) and still nothing moved, the player spawned at 0, 0, 0.
Thank you anyway, though, I appreciate the help.
Your answer
Follow this Question
Related Questions
Updating localPosition using parents' position 2 Answers
Adding a value to transform.position.y runs very untrusted 0 Answers
How to make object follow y-axis of another object. 1 Answer
2D RPG boomerang instantiation help. 2 Answers
Unity3D - Playback object array of position (with dynamic velocity) 0 Answers