【问题标题】:change text based on button-click基于按钮单击更改文本
【发布时间】:2015-02-21 04:33:47
【问题描述】:

我正在使用 Unity3d

我试图追踪问题的根源,但没有成功。 “猜测”中的文本更新在通过按键完成时正确发生,但通过单击按钮不会发生(均登录异常)。

The code:

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

public class NumberWizard : MonoBehaviour
{
    public Text guessText;
    int min;
    int max;
    int guess;

    public void Higher ()
    {
        min = guess;
        NextGuess ();
    }

    public void Lower ()
    {
        max = guess;
        NextGuess ();
    }

    public void Correct ()
    {
        print ("I won!");
        StartGame ();
    }

    private void StartGame ()
    {
        min = 1;
        max = 1000;
        guess = 500;
        guessText.text = guess.ToString();
        print (guess);
    }

    private void NextGuess ()
    {
        guess = (max + min) / 2;
        guessText.text = guess.ToString();
        print (guess);
    }


    // Use this for initialization
    void Start ()
    {
        StartGame ();
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown (KeyCode.UpArrow)) {
            Higher ();
        } else if (Input.GetKeyDown (KeyCode.DownArrow)) {
            Lower ();
        } else if (Input.GetKeyDown (KeyCode.Return)) {
            Correct ();
        }

    }
}

顺便说一句,我已经将按钮放在另一个名为“LevelManager”的控制器上。我在类声明上方添加了[RequireComponent(typeof(LevelManager))],但它不起作用。

基本上,它在guessText.text =guess.ToString(); object 未设置为实例,但我在 Unity3d 中设置了引用特定文本并且它应该使用 NumberWizard.cs

【问题讨论】:

    标签: button text unity3d unity3d-gui


    【解决方案1】:

    创建此脚本时,需要将其附加到GameObject。完成编码后,它应该附加到一个空游戏对象。然后,当您从编辑器中选择脚本时,您的公共变量 Text guessText 将在编辑器上可用,现在您必须将您创建的 GUIText 拖放到 guessText 属性上。

    【讨论】:

      【解决方案2】:

      错误表明您必须初始化变量,无论是通过执行“新建”还是将其分配给 Dinal 所说的游戏内拖放对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-04
        相关资源
        最近更新 更多