Answer the question
In order to leave comments, you need to log in
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";
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question