# How can i change transform position only on the x each time by gap of 50?

Global variable gap:

public int gap = 10;


Then:

clone.position = new Vector3(transform.position.x, 0,0);


clone is Transform

I want to use the gap variable to make spaces between each clone:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class ClickOnKeys : MonoBehaviour
{
public GameObject[] prefabs;
public int gap = 10;

private GameObject go;

void Start()
{
prefabs = Resources.LoadAll("Prefabs", typeof(GameObject)).Cast<GameObject>().ToArray();

foreach (GameObject prefab in prefabs)
{
if (prefab.name == "Test")
{
go = prefab;
}
}
}

void Update()
{
if (Input.anyKey)
{
foreach (Transform child in go.transform)
{
if (Input.inputString == child.name)
{
Transform clone = Instantiate(child);
clone.parent = transform;
clone.position = new Vector3(transform.position.x + gap += 50, 0,0);
}
}
}
}
}


I tried to make:

clone.position = new Vector3(transform.position.x + gap += 50, 0,0);


But getting error on the part: transform.position.x + gap

The left-hand side of an assignment must be a variable, property or indexer The

This is what i tried now:

Transform clone = Instantiate(child);
clone.parent = transform;
gap += 50;
clone.position = new Vector3(transform.position.x - gap, 0, 0);


But the result is that the space/gap between the clones is not equal and also it seems the clones that each one is a letter looks rotated like mirror:

Each clone present a letter so it should show the word Hello But the letter e is on the H and the gap/space between the first l and second l not equal and the l is on the o: That's not valid C#. You probably want to do something like this instead:

clone.position = new Vector3(transform.position.x + gap, 0,0);
gap += 50;


This will give you a gap of 10 units between the first and second clone, and 50 units between the second and the third, and the third and the fourth, and so on.

So if you don't want that, you might want to use two variables instead, one containing the offset and one (constant) storing the difference. So something like

offset += gapSize;
clone.position = new Vector3(transform.position.x + offset, 0,0);


where offset is initialized to 0, and gapSize is set to 50.

• I tried it but the result is not good yet. I updated my question now with what i tried and what is the result. – user1196715 Oct 8 '17 at 12:31
• You should try the second approach I showed. The second paragraph of my answer clarifies how your current approach leaves the first gap different from the rest. – Tyyppi_77 Oct 8 '17 at 12:46