Text.text属性不会显示

时间:2018-02-11 06:01:25

标签: c# unity3d

所以我在Unity制作了一个乒乓球游戏,但是我遇到了编辑文本的问题。我有一个带有UI文本的画布。我的代码的问题在于我的public Text mytext:MonoDevelop或Visual Studio无法找到mytext.text的定义。我的代码无法编译。

我已经加入了using UnityEngine.UI,但仍然无效。

这是我的代码:

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class balls : MonoBehaviour 
{
    public Text mytext;
    public bool left;
    public bool upwards;

    void Update ()
    {
        if (left == true) {
            gameObject.transform.Translate (Vector3.left * 6 * Time.deltaTime);
        } else 
        { 
            gameObject.transform.Translate (Vector3.right * 6 * Time.deltaTime);
        }
        if (gameObject.transform.position.y > 4.2 || gameObject.transform.position.y < -4.2) 
        {
            if (upwards == true) {
                upwards = false;
            } else 
            {
                upwards = true;
            }
        }
        if (upwards == true) 
        {
            gameObject.transform.Translate (Vector3.up * 6 * Time.deltaTime);
        } else
        {
            gameObject.transform.Translate (Vector3.down * 6 * Time.deltaTime);
        }
        if (gameObject.transform.position.x > 7.82 || gameObject.transform.position.x < -7.82) 
        {
            SceneManager.LoadScene ("Pong");
        }
    }
    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "Paddle")
        {
            mytext.text = "4";
            if (left == true) {
                left = false;
            } else 
            {
                left = true;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

确保 -

将文本对象拖到编辑器中脚本的mytext文本槽中。

运行场景,可以触发OnTriggerEnter,获取的gameObject标记为Paddle

修改

避免使用同名的自定义类。

https://answers.unity.com/questions/1340078/type-text-does-not-contain-a-definition-for-text.html

答案 1 :(得分:0)

尝试制作

var vid =  $(this).attr("href");

有时Unity会解决这个我不知道的问题。