I want to create game like Osu!Mania or FNF. I have Canvas and Spawner Gameobject, and Button Prefab in Assets. When I compile this code, Unity creates additional Canvas for each button.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
public class ButtonSpawner : MonoBehaviour
{
public GameObject buttonPrefab;
public int buttonCount = 9;
public float spacing = 2.1f;
public string[] buttonLabels = { "a", "s", "d", "f", "space", "h", "j", "k", "l" };
void Start()
{
SpawnButtons();
}
void SpawnButtons()
{
for (int i = 0; i < buttonCount; i++)
{
Vector3 position = new Vector3((i-4) * spacing, 0, 0);
GameObject button = Instantiate(buttonPrefab, position, Quaternion.identity);
button.transform.SetParent(transform, false);
button.GetComponentInChildren<TMP_Text>().text = buttonLabels[i];
string label = buttonLabels[i];
button.GetComponent<Button>().onClick.AddListener(() => OnButtonPressed(label));
}
}
void OnButtonPressed(string label)
{
Debug.Log("Button YEEE: " + label);
}
}```
Unity makes a Canvas automatically and because of that positions of TMpro are awful
[](https://i.sstatic.net/B2GdVKzu.png)
I tried to use ChatGPT, but it's already broken and suggesting to create more Canvases, but doesn't work. I also couldn't find any information about that in the internet, maybe I can't search...