- Home /
How can i position gameobject one next the other with equal gap/space between them considering the gameobject width ?
The problem is that each Prefab have another width size so even if i set the gap between them to 50 and it is 50 since not all prefabs same width it looks like the gap between them is not the same:
In the screenshot you can see on the bottom: The Island And on the top of it you can see: Hello World The: The Island is not the same script but it show what i mean to make the prefabs to be the same gap. That is what i want the: Hello World to be like.
I'm setting in the script a gap of 50 and when i check the prefabs/gameobjects positions the gap between them is indeed 50 the problem is that the GameObject H is not same width like l or like o or like r

What i want is to make the Hello World to be same like The Island when i position the gameobjects. Each letter is a prefab: H is prefab E is prefab l and l each are prefab o is prefab....
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 = 50;
public int offset = 0;
private GameObject go;
void Start()
{
prefabs = Resources.LoadAll("Prefabs", typeof(GameObject)).Cast<GameObject>().ToArray();
foreach (GameObject prefab in prefabs)
{
if (prefab.name == "Letters")
{
go = prefab;
}
}
transform.localPosition = new Vector3(0,0,0);
}
void Update()
{
if (Input.anyKey)
{
foreach (Transform child in go.transform)
{
if (Input.inputString == child.name)
{
Transform clone = Instantiate(child);
clone.parent = transform;
clone.transform.eulerAngles = new Vector3(0, 0, 0);
offset += gap;
clone.transform.position = new Vector3(-offset, 0, 0);
}
}
}
}
}
Just a question, but why not just create a UI text element, and then modify the text attribute of it? Anyway if you didn't want to do that, you could simply attach a script to each prefab which holds a public reference to the width of the character. Then inside the script where you spawn the text, you take into account the width using this reference:
prefab[n].GetComponent<script_name>().width
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers
How can i set the same script on two GameObjects so the script will work on both objects same time ? 1 Answer
How do i change the GUI.Box font size and box size ? 0 Answers