Instatiate GameObject and immediate set it's posistion seems doesn't work


So I want to fairy to spawn in a formation of Left 4 and Right 4 but it seems there is always one got offset in Y axis position. As indicated by the arrow I add where they should be.

public class GreenFairyFormation2 : MonoBehaviour {

    public GameObject GreenFairy1;
    public GameObject GreenFairy2;
    public GameObject GreenFairy3;
    public GameObject GreenFairy4;
    public GameObject GreenFairy5;
    public GameObject GreenFairy6;
    public GameObject GreenFairy7;
    public GameObject GreenFairy8;

  
    Vector2 LeftFormationStartPos = new Vector2(-7f, 4.8f);
    Vector2 LeftFormationStartPos1 = new Vector2(-6.5f,4.8f);
    Vector2 LeftFormationStartPos2 = new Vector2(-6f, 4.8f);
    Vector2 LeftFormationStartPos3 = new Vector2(-5.5f, 4.8f);
  

    Vector2 RightFormationStartPos = new Vector2(7f, 4.8f);
    Vector2 RightFormationStartPos1 = new Vector2(6.5f, 4.8f);
    Vector2 RightFormationStartPos2 = new Vector2(6f, 4.8f);
    Vector2 RightFormationStartPos3 = new Vector2(5.5f, 4.8f);
    // Use this for initialization
    void Start ()
    {
        InvokeRepeating("LeftFormation", 8f, 0.8f);
        InvokeRepeating("RightFormation", 8.5f, 0.8f);

    }
  
    // Update is called once per frame
    void Update ()
    {
        if(StageTimerControl.TimerClick > 15f)
        {
            CancelInvoke("LeftFormation");
            CancelInvoke("RightFormation");
        }
    }
    void LeftFormation()
    {
        Instantiate(GreenFairy1);
        GreenFairy1.transform.position = LeftFormationStartPos;
        Instantiate(GreenFairy2);
        GreenFairy2.transform.position = LeftFormationStartPos1;
        Instantiate(GreenFairy3);
        GreenFairy3.transform.position = LeftFormationStartPos2;
        Instantiate(GreenFairy4);   
        GreenFairy4.transform.position = LeftFormationStartPos3;

     
    }
    void RightFormation()
    {
        Instantiate(GreenFairy5);
        GreenFairy5.transform.position = RightFormationStartPos;
        Instantiate(GreenFairy6);
        GreenFairy6.transform.position = RightFormationStartPos1;
        Instantiate(GreenFairy7);
        GreenFairy7.transform.position = RightFormationStartPos2;
        Instantiate(GreenFairy8);
        GreenFairy8.transform.position = RightFormationStartPos3;  
    }
}

So I have Instatiate Object and right after it to set it’s position,I don’t think this the best way but should they should be in one line, having same y right?

You are trying to set the position of the prefab GameObjects. The Instantiate method will return the newly created GameObject and you want to set its position as follows:

   void LeftFormation()
   {
        GameObject Obj1 = Instantiate(GreenFairy1);
        Obj1.transform.position = LeftFormationStartPos;
        .
        .
        .
   }

The Instantiate method also has versions that allows you to set the position without having to set it yourself later.

You are setting the position of the object prefab not the physical GameObject.

You need to grab the return value of the Instantiate function, cast it to a GameObject. You then set the position of this object.

This also means you won’t need so many GreenFairy variables. You will only need one to hold the prefab of a single green fairy.

So what I am doing is changing the orginal prefab position when it Instantiate not the instance of the prefab?

yup

Thank you, that solve many problem. But I still can’t really figure out why it got offset?