致力于熟悉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元素的引用?
[您需要从另一个脚本设置myText
实例的handle
值,或者在选择添加了handle
组件的GameObject时,在Unity编辑器的“检查器”窗口中进行设置。