首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Text.text属性未显示

Text.text属性未显示
EN

Stack Overflow用户
提问于 2018-02-11 14:01:25
回答 2查看 276关注 0票数 0

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

我已经包含了using UnityEngine.UI,但是仍然不能工作。

下面是我的代码:

代码语言:javascript
复制
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;
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-02-11 14:21:36

确保-

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

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

编辑:

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

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

票数 0
EN

Stack Overflow用户

发布于 2018-02-13 14:35:05

试着去做吧

代码语言:javascript
复制
UnityEngine.Text myText;

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48728803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档