Как дать каждой кнопке свой индекс?

Как при создании кнопок через скрипт присвоить каждой кнопке свой индекс/тег

У меня загружается уровень на последнем значении. Хотелось что бы значение было не последним а номером кнопки

button 1 - Level1 button 2 - Level2

сейчас любая кнопка загружает 10 уровень ( последнее значение )

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ButtonCreate : MonoBehaviour
{
    public Transform canvas;
    public Font font;
    public Sprite texture;
    public float scale;
    public int maxButton = 10;

    string number;

    private int presss;

    private int value = 0;


    GameObject index; 
    GameObject newText;

    void Start()
    {
        for(int i = 0; i<maxButton; i++)
        {
              CreateButton();
        }

    }


    void Update()
    {
        number = value.ToString();
        Debug.Log(newText);
    }

    void CreateButton ()
    {

        value++;
        number = value.ToString();
        GameObject newButton = new GameObject("New Button", typeof(Image), typeof(Button), typeof(LayoutElement));

        newButton.tag = "Player" + number;
        newButton.transform.localScale = new Vector3 (scale, scale, scale);
        newButton.transform.SetParent(canvas);
        newButton.GetComponent<LayoutElement>().minWidth = 35;
        newButton.GetComponent<Image>().sprite = texture;
        newText = new GameObject(""+value, typeof(Text));
        newText.transform.SetParent(newButton.transform);
        newText.GetComponent<Text>().text = ""+value; 
        newText.GetComponent<Text>().font = font;
        newText.GetComponent<Text>().fontSize = 80;
        RectTransform rt = newText.GetComponent<RectTransform>();
        rt.anchorMin = new Vector2(0,0);
        rt.anchorMax = new Vector2(1,1);
        rt.anchoredPosition = new Vector2(0,0);
        rt.sizeDelta = new Vector2(0,0);

        newText.GetComponent<Text>().color = new Color(1,1,1);
        newText.GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
        newButton.GetComponent<Button>().onClick.AddListener(delegate{press();});
    }

    public void press ()
    {
       SceneManager.LoadScene("Level"+this.GetComponent<Text>().text);
    }




}

Ответы (0 шт):