Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by 769270865, Dec 9, 2016.

  1. 769270865

    769270865

    Joined:
    Feb 10, 2016
    Posts:
    23
    QQ截图20161208165212.jpg
    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.

    Code (CSharp):
    1. public class GreenFairyFormation2 : MonoBehaviour {
    2.  
    3.     public GameObject GreenFairy1;
    4.     public GameObject GreenFairy2;
    5.     public GameObject GreenFairy3;
    6.     public GameObject GreenFairy4;
    7.     public GameObject GreenFairy5;
    8.     public GameObject GreenFairy6;
    9.     public GameObject GreenFairy7;
    10.     public GameObject GreenFairy8;
    11.  
    12.  
    13.     Vector2 LeftFormationStartPos = new Vector2(-7f, 4.8f);
    14.     Vector2 LeftFormationStartPos1 = new Vector2(-6.5f,4.8f);
    15.     Vector2 LeftFormationStartPos2 = new Vector2(-6f, 4.8f);
    16.     Vector2 LeftFormationStartPos3 = new Vector2(-5.5f, 4.8f);
    17.  
    18.  
    19.     Vector2 RightFormationStartPos = new Vector2(7f, 4.8f);
    20.     Vector2 RightFormationStartPos1 = new Vector2(6.5f, 4.8f);
    21.     Vector2 RightFormationStartPos2 = new Vector2(6f, 4.8f);
    22.     Vector2 RightFormationStartPos3 = new Vector2(5.5f, 4.8f);
    23.     // Use this for initialization
    24.     void Start ()
    25.     {
    26.         InvokeRepeating("LeftFormation", 8f, 0.8f);
    27.         InvokeRepeating("RightFormation", 8.5f, 0.8f);
    28.  
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update ()
    33.     {
    34.         if(StageTimerControl.TimerClick > 15f)
    35.         {
    36.             CancelInvoke("LeftFormation");
    37.             CancelInvoke("RightFormation");
    38.         }
    39.     }
    40.     void LeftFormation()
    41.     {
    42.         Instantiate(GreenFairy1);
    43.         GreenFairy1.transform.position = LeftFormationStartPos;
    44.         Instantiate(GreenFairy2);
    45.         GreenFairy2.transform.position = LeftFormationStartPos1;
    46.         Instantiate(GreenFairy3);
    47.         GreenFairy3.transform.position = LeftFormationStartPos2;
    48.         Instantiate(GreenFairy4);  
    49.         GreenFairy4.transform.position = LeftFormationStartPos3;
    50.  
    51.      
    52.     }
    53.     void RightFormation()
    54.     {
    55.         Instantiate(GreenFairy5);
    56.         GreenFairy5.transform.position = RightFormationStartPos;
    57.         Instantiate(GreenFairy6);
    58.         GreenFairy6.transform.position = RightFormationStartPos1;
    59.         Instantiate(GreenFairy7);
    60.         GreenFairy7.transform.position = RightFormationStartPos2;
    61.         Instantiate(GreenFairy8);
    62.         GreenFairy8.transform.position = RightFormationStartPos3;  
    63.     }
    64. }
    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?
     
  2. Dennis59

    Dennis59

    Joined:
    Jan 8, 2013
    Posts:
    66
    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:

    Code (csharp):
    1.  
    2.    void LeftFormation()
    3.    {
    4.         GameObject Obj1 = Instantiate(GreenFairy1);
    5.         Obj1.transform.position = LeftFormationStartPos;
    6.         .
    7.         .
    8.         .
    9.    }
    10.  
    The Instantiate method also has versions that allows you to set the position without having to set it yourself later.
     
  3. magnite

    magnite

    Joined:
    Dec 12, 2012
    Posts:
    125
    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.
     
  4. 769270865

    769270865

    Joined:
    Feb 10, 2016
    Posts:
    23
    So what I am doing is changing the orginal prefab position when it Instantiate not the instance of the prefab?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  6. 769270865

    769270865

    Joined:
    Feb 10, 2016
    Posts:
    23
    Thank you, that solve many problem. But I still can't really figure out why it got offset?