如何正确使用int更改Unity中的文本

问题描述

我正在尝试创建一个文本框,其中一个名为“ Dugo”的字符会告诉播放器某些内容,但是它不起作用。该脚本应该根据int值更改文本的内容。所有按钮和文本均已正确连接到脚本,我将其更改为double,但仍然无法正常工作。有人可以帮忙吗?

代码如下:

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

public class DugoText1 : MonoBehavIoUr
{

    public double text = 0;
    public GameObject DugoBox;
    public GameObject DugoText;

    public void ClickTheButton()
    {

        text += 1;

    }

    void Update()
    {
        if (text == 1) {
            DugoText.GetComponent<Text>().text = "text1";
        }
        if (text == 2)
        {
            DugoText.GetComponent<Text>().text = "text2";
        }
        if (text == 3)
        {
            DugoText.GetComponent<Text>().text = "text3";
        }
        if (text == 4)
        {
            DugoText.GetComponent<Text>().text = "text4";
        }
        if (text == 5)
        {
            DugoText.GetComponent<Text>().text = "text5";
        }
        if (text == 6)
        {
            DugoText.GetComponent<Text>().text = "text6";
        }
    }


}

解决方法

我测试了您的脚本,这就是我得到的-(单击这些以查看图像)

Attach Dugo Script to a gameObject and attach said gameObject(not the script) to a Button's "On Click" event,drag the Text component to DugoScript gameObject's "Dugo Text" field under DugoScript1

Run the game and press the button - text changes successfully