how can I set position.x by code - help?

Hello everyone.

I have a horizontal list of gameobjects in my scene. I want these objects to each have the same amount of distance between them along the X axis when the game is playing no matter how and where they are dragged to in the scene. Right now i entered this code to the gameobjects to have the y positions set to 0:
transform.position.y = 0;

But what i would like to do is drag each gameobject into the scene and then the code would position the objects in a horizontal line along the X axis but each with the same distance between them.

eg. object1 would be at (0,0), object2 at (2,0), object3 at (4,0), and so on, and this will all be done automatically through code.

I have already added the gameobjects to the scene, and i’m just wondering is this can be done?
This is a 2D game.
Any help would be deeply appreciated.

This should get you started (C#)

for (int i = 0; i < 10; i++){
    Instantiate (myPrefab, new Vector3(i*2,0,0), Quaterion.identity);

Are you familiar with Tags ? Here, and here.

What I would do would is set all those game objects to the specific tag you want (I’ll name it Alignment for now, you can name it anyway you want)

then, in a script, that’s on a separate game object I would do something like this :

public int incrementValue = 2;

void Start()
    Vector3 pos =;
    int i = 0;
    foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Alignment"))
        pos.x = i;
        obj.transform.position = pos;
        i += incrementValue;