【发布时间】:2019-11-05 15:18:19
【问题描述】:
致力于熟悉 C# 和统一开发。今天,我正在努力在我的脚本中获取对 Text 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 元素的引用,需要改进哪些方面?
【问题讨论】:
-
这个错误通常是由于忘记在inspector中分配GameObject引起的。您是否已将文本 UI 游戏对象拖放到检查器中的“我的文本”字段中以获取句柄组件?
标签: c# unity3d game-engine