所以我在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;
}
}
}
}发布于 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
发布于 2018-02-13 14:35:05
试着去做吧
UnityEngine.Text myText;有时候Unity会解决这个问题,我不知道。
https://stackoverflow.com/questions/48728803
复制相似问题