using UnityEngine;
using TMPro;

public class SpawnText : MonoBehaviour
{
    public GameObject textPrefab;
    public string textToSpawn = "GRAVITY";
    public float letterSpacing = 2f;

    void Start()
    {
        SpawnTextLetters();
    }

    void SpawnTextLetters()
    {
        // total width of string
        float totalWidth = textToSpawn.Length * letterSpacing;

        // spawn letter and position it (offset)
        Vector3 spawnPosition = transform.position - new Vector3(totalWidth / 2f,0,0);

        // loop to make the full word/sentence
        foreach(char letter in textToSpawn)
        {
            GameObject letterObject = Instantiate(textPrefab,spawnPosition,Quaternion.identity);
            letterObject.GetComponent<TextMeshPro>().text = letter.ToString();

            // adjust to the right
            spawnPosition.x += letterSpacing;
        }
    }
}