N
N
NikitaSova2021-01-20 10:12:37
Unity
NikitaSova, 2021-01-20 10:12:37

How to change text in Unity 2D via script?

Hi all. I recently started getting familiar with Unity 2d and decided to make a simple program with two buttons and text. The bottom line is that by pressing the first button the text should change to the number 1 and by pressing the second button to the number 2. And I am faced with the fact that I do not understand how to change the text through the script. I did not find a solution on the Internet (nothing works). Help a newbie.

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

public class Button : MonoBehaviour
{
    public GameObject Text1;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void Button1(object sender)
    {
        Text1.GetComponent<Text>().text = "1";
    }
    private void Button2(object sender)
    {
        Text1.GetComponent<Text>().text = "2";
    }
}

6007d76a2be11960939439.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question