简体   繁体   English

Unity3D C#-文本UI

[英]Unity3D C# - Text UI

I have the following piece of code. 我有以下代码。 I referenced the text component in the editor to no_lives. 我在编辑器中将文本组件引用为no_lives。 The gamemanager (singleton) is being instantiated a scene before. 游戏经理(单身)之前被实例化为场景。 The debug.log() shows 5 in the console. debug.log()在控制台中显示5。 But when I try to set the text I get that the reference is not set to an instance of the object. 但是当我尝试设置文本时,我得到的是引用未设置为对象的实例。 Why is that? 这是为什么?

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

public class level1_script : MonoBehaviour {

    public Text no_lives;

    // Use this for initialization
    void Start () {
        no_lives = GetComponent<Text>();
    }

    // Update is called once per frame
    void Update () {
        int lives_n = gamemanager.lives_f ();
        Debug.Log (lives_n);
        no_lives.text = lives_n + " x";
    }
}

Are you certain that Start() was called before Update and that the GetComponent is actually setting a value to no_lives ? 您确定Start()Update之前被调用并且GetComponent实际上将值设置为no_lives吗?

If not then code defensively. 如果不是,则进行防御性编码。 Also lives_n needs to be converted to a string lives_n.ToString() + " x" or use a format. 另外, lives_n需要转换为字符串lives_n.ToString() + " x"或使用格式。

void Update () {
    int lives_n = gamemanager.lives_f ();
    Debug.Log (lives_n);
    if(no_lives != null) {
        no_lives.text = string.Format("{0} x", lives_n);
    }
}

EDIT: 编辑:

But when I try to set the text I get that the reference is not set to an instance of the object. 但是当我尝试设置文本时,我得到的是引用未设置为对象的实例。

That's because when you do GetComponent<Text>‌​() , it will look for the instance of Text component on the-same GameObject your level1_script script is attached to. 这是因为当您执行GetComponent<Text>‌​() ,它将在您的level1_script脚本所附加的相同 level1_script上查找Text组件的实例。

You have two choices: 您有两种选择:

1 .Attach Text component that GameObject your level1_script script is atached to then your current code should work. 1。将 GameObject和您的level1_script脚本连接到Text组件,然后您的当前代码应该可以正常工作。

2 .If you already have the Text component attached to another GameObject, let's say an Object called "MyText" , use GameObject.Find to find "MyText" then perform GetComponent on it to get the Text component. 2。如果你已经拥有了Text连接到另一个游戏物体组件,让我们说所谓的“MYTEXT”对象,请使用GameObject.Find找到“MYTEXT”然后执行GetComponent它来得到Text组件。

no_lives = GameObject.Find("MyText").GetComponent<Text>‌​()

Edit: 编辑:

Well, both Text and script are attached to the same object prntscr.com/f7p7vx 好吧,文本和脚本都附加到同一对象prntscr.com/f7p7vx

It's likely attached to multiple GameObject and the other GameObject does not have the Text script attached to it. 它可能已附加到多个GameObject,而另一个GameObject没有附加Text脚本。

Select the level1_script script, go to Assets --> Find References in Scene then remove the duplicated script from other objects. 选择level1_script脚本,转到“ 资产 -> 在场景中查找引用”,然后从其他对象中删除重复的脚本。

As the script logs '5' in the debug log, it seems to me that the variable lives_n is used correctly. 当脚本在调试日志中记录“ 5”时,在我看来,变量lives_n的使用正确。 My guess is that the component "no_lives" isn't filled (that's what is often causing the "reference is not set to an instance of the object" error) I suggest that you try to add the script to the UI Text object in your scene, this way the script finds the "Text" component on that object. 我的猜测是未填充组件“ no_lives”(这通常是导致“未将引用设置为对象的实例”错误)的原因,建议您尝试将脚本添加到您的UI Text对象中场景,这样脚本可以在该对象上找到“文本”组件。

It's important to know the differences between objects and components, because they're both essential parts of unity. 了解对象和组件之间的差异非常重要,因为它们都是统一性的重要组成部分。 Objects are "things" in your scene, like cubes, cameras and stuff like that. 对象是场景中的“事物”,例如立方体,照相机和类似的东西。 Components on the other hand are part of object and define the properties of that object. 另一方面,组件是对象的一部分,并定义该对象的属性。 For example: a camera object has a camera component to make it a camera and to make it actually viewable by the user. 例如:照相机对象具有照相机组件,以使其成为照相机并使用户实际可见。 The GetComponent<>() method searches for a (in your case) Text component on the object the script is attached to. GetComponent <>()方法在脚本附加到的对象上搜索(在您的情况下)Text组件。

Hope this helps! 希望这可以帮助! (oh and don't forget to remove the script from the other object you've placed it on, in case this works. Otherwise you'll still get errors) (哦,别忘了从脚本上放置的其他对象中删除该脚本,以防万一这可行。否则,您仍然会收到错误消息)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM