简体   繁体   English

unity c# - NullReferenceException:未将对象引用设置为对象的实例

[英]Unity c# - NullReferenceException: Object reference not set to an instance of an object

Im having an error during runtime with my image and text .我的imagetextruntime error I have both objects created in my hierarchy named correctly but apparently my script cant find them.我在我的hierarchy创建的两个对象命名正确,但显然我的脚本找不到它们。 Why isn't what i have written not sufficient?为什么我写的还不够? All i get in the error is Object reference not set to an instance of an object, but i don't understand why i need to create an instance of this object when i have one in my hierarchy.我在error得到的只是Object reference未设置为Object reference的实例,但我不明白为什么当我的层次结构中有一个对象时我需要创建该对象的实例。 I'm still relatively new to both unity and c# and couldn't find any real explanatory answer by searching and the unity tutorials weren't very helpful since I'm sure its just a basic question about c#我对 unity 和c#仍然相对较新,通过搜索找不到任何真正的解释性答案,并且 unity 教程不是很有帮助,因为我确定它只是一个关于c#的基本问题

private Text serverInfoText;
private GameObject serverInfoImage;

void Awake()
{
    serverInfoImage = GameObject.Find ("ServerInfoImage");
    serverInfoImage.SetActive (false);
    serverInfoText = GameObject.Find("ServerInfoText").GetComponent<Text>();
}

Impossible to say without looking at your scene.不看你的场景就无法说。

Things to try:尝试的事情:

  • GameObject.Find only works on active game objects. GameObject.Find仅适用于活动的游戏对象。 Make sure the game objects are not greyed out in your scene view.确保游戏对象在场景视图中没有变灰。
  • Double check the name strings are correct and use Debug.Log to sanity check.仔细检查名称字符串是否正确并使用Debug.Log进行完整性检查。
  • You don't mention which line the actual exception occurs.您没有提到实际发生的异常发生在哪一行。 In your above code, it's possible to throw an exception in two locations: on SetActive() if serverInfoImage is null, or on GetComponent<Text>() if the ServerInfoText object is not found.在上面的代码中,有可能在两个位置抛出异常:如果serverInfoImage为 null,则在SetActive()如果未找到 ServerInfoText 对象,则在GetComponent<Text>() Read the actual exception message you get and make sure you are looking at the correct line/object.阅读您获得的实际异常消息,并确保您查看的是正确的行/对象。
  • Since your objects are already created in your scene, there's no advantage to using GameObject.Find to get references to the objects.由于您的对象已经在您的场景中创建,因此使用GameObject.Find获取对对象的引用没有任何优势。 It would be more efficient to directly set the references to the objects via the inspector.通过检查器直接设置对对象的引用会更有效。 This option has the added benefit of working with disabled GameObjects.此选项具有使用禁用游戏对象的额外好处。 Either make your two member variables private, or use the SerializeField attribute to make the properties visible in the inspector.要么将您的两个成员变量设为私有,要么使用SerializeField属性使属性在检查器中可见。

暂无
暂无

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

相关问题 未处理C#NullReferenceException-未将对象引用设置为对象的实例 - C# NullReferenceException was unhandled - Object reference not set to an instance of an object Unity 中的 Json 序列化 - NullReferenceException:未将对象引用设置为对象的实例 - Json Serialization in Unity - NullReferenceException : Object reference not set to an instance of an object NullReferenceException:Object 引用未设置为 object Unity2D 的实例 - NullReferenceException: Object reference not set to an instance of an object Unity2D Unity5 NullReferenceException:对象引用未设置为对象CoapManager的实例 - Unity5 NullReferenceException: Object reference not set to an instance of an object CoapManager Unity 切换抛出 NullReferenceException:未将对象引用设置为对象的实例 - Unity toggle throws NullReferenceException: Object reference not set to an instance of an object 对象引用未设置为对象Unity3D C#的实例 - Object reference not set to instance of an object Unity3D c# 未将Unity C#对象引用设置为对象的实例(射击脚本) - Unity c# Object reference not set to an instance of an Object ( Shooting Script ) Unity C#对象引用未设置为对象的实例 - Unity C# Object reference not set to an instance of an object Unity3d C#“对象引用未设置为对象的实例”? - Unity3d c# “Object reference not set to an instance of an object”? System.NullReferenceException-对象引用未设置为对象的实例。 C#中的原因 - System.NullReferenceException - Object reference not set to an instance of an object. Causes in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM