如何:正确引用脚本中的文本UI组件

问题描述 投票:0回答:1

致力于熟悉C#和统一开发。今天,我正在努力在脚本中获取对文本UI对象的引用。下面的代码会产生此错误:

NullReferenceException: Object reference not set to an instance of an object
handle.Awake () (at Assets/handle.cs:20)

脚本看起来像这样:

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

public class handle : MonoBehaviour
{

    public Text myText;

    // Start is called before the first frame update
    void Start()
    {

    }

    void Awake()
    {
        myText.text = "@organickoala718" ;
    }

    // Update is called once per frame
    void Update()
    {

    }
}

需要进行哪些改进才能正确获得对Text UI元素的引用?

c# unity3d game-engine
1个回答
0
投票

[您需要从另一个脚本设置myText实例的handle值,或者在选择添加了handle组件的GameObject时,在Unity编辑器的“检查器”窗口中进行设置。

© www.soinside.com 2019 - 2024. All rights reserved.