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. Dismiss Notice

[C#]How do I spawn Gameobjects beside eachother.

Discussion in 'Scripting' started by Radiag, May 17, 2014.

  1. Radiag

    Radiag

    Joined:
    Feb 16, 2013
    Posts:
    3
    So, im trying to spawn my Aliens as a Child and that works fine, but how do I get them to spawn beside eachother now instead of at the 0,0,0 position of the parent.
    This is my current code to instantiate them as a Child

    Code (csharp):
    1.  
    2. public int AmmountAliens = 8;
    3. public GameObject Aliens;
    4. public GameObject AliensParent;
    5.  
    6. void Start ()
    7. {
    8.         for(int i = 0; i < AmmountAliens; i++)
    9.         {
    10.             GameObject obj;
    11.             obj = (GameObject)Instantiate(Aliens, AliensParent.transform.position, Aliens.transform.rotation);
    12.             obj.transform.parent = AliensParent.transform;
    13.         }
    14. }
    15.  
    sorry if my question isnt completly understandable, getting late over here :D
     
    Last edited: May 17, 2014
  2. m3taphysics

    m3taphysics

    Joined:
    Oct 9, 2012
    Posts:
    23

    After you have assigned them to their parent. You can use transform.localPosition to move them relative to their parent.
     
  3. Radiag

    Radiag

    Joined:
    Feb 16, 2013
    Posts:
    3
    now i get the error " 'UnityEngine.Vector3' is a 'type' but is used like a 'variable' "
    Code (csharp):
    1.  
    2. transform.localPosition = Vector3(0,0,0);
    3.  
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Code (csharp):
    1.  
    2. // Need the new keyword to create the Vector3.
    3. transform.localPosition = new Vector3(0,0,0);
    4.  
     
  5. Radiag

    Radiag

    Joined:
    Feb 16, 2013
    Posts:
    3
    So I changed it to
    Code (csharp):
    1.  
    2. transform.localPosition = new Vector3 (i,0,0);
    3.  
    The Error is gone but somehow nothing is happening in game.

    Ohh forgot to add the obj.(...) working now, tyvm guys!
     
    Last edited: May 17, 2014