简体   繁体   English

Unity C#:自定义对象为空,但是我可以访问其属性

[英]Unity C# : custom Object is null BUT I can access its properties

I found a way to get around this issue in the code, but I would like to understand why this is happening because I want to learn C# well. 我在代码中找到了解决此问题的方法,但是我想了解为什么会这样,因为我想很好地学习C#。 I´ve searched a lot and could simplify the problem, here's the simplified code: 我进行了很多搜索,可以简化问题,这是简化的代码:

/*MyCustomComponent.cs, this is attached to a game object in the scene*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MyCustomComponent : MonoBehaviour {
    void Start () {
        Test testInstance = new Test();
        Debug.Log (testInstance);
        Debug.Log (testInstance == null);
        Debug.Log (testInstance.Equals(null)); //Also tried the same with Equals()
        Debug.Log (testInstance.value);
    }
}

/*Test.cs*/
using UnityEngine;
using System.Collections;

public class Test : Object {
    public int value = 7;
    public Test(){

    }
}

Console Output: 控制台输出:

null
true
7

Why is this happening? 为什么会这样呢? How can be possible an Object is null but returns a property value? Object如何可能为null但返回属性值?

Because Unity.Object works in a very peculiar way, basically it contains data regarding the managed object while pointing to the native object (crudely speaking). 因为Unity.Object工作方式非常特殊,所以基本上它包含指向托管对象的数据,同时指向本地对象(粗略地说)。

Let's make an example: you create a new object inheriting from MonoBehaviour . 让我们举个例子:您创建一个继承自MonoBehaviour的新对象。 Now you have two objects, one on the native side and one on the managed side. 现在,您有两个对象,一个在本机端,一个在托管端。 You can access the instanceID propriety of the managed object thanks to the fact that MonoBehaviour inherits from Unity.Object . 由于MonoBehaviour继承自Unity.Object ,因此您可以访问托管对象的instanceID适当性。 Basically, the latter is used to show all the relative data of the object in the Inspector. 基本上,后者用于显示检查器中对象的所有相关数据。

Now, you want to Destroy this game object by calling the same named method. 现在,您要通过调用相同的命名方法来Destroy此游戏对象。 The native object is actually destroyed, but the managed object is not, since all managed objects can only be destroyed by the garbage collector. 实际上,本机对象已被销毁,但托管对象并未被销毁,因为所有托管对象只能由垃圾收集器销毁。 It's at this point that Unity.Object becomes null : it's a trick of the Unity engine uses in order to avoid that you try to access a native object which doesn't exist anymore. 此时, Unity.Object变为null :这是Unity引擎使用的技巧,以避免您尝试访问不再存在的本机对象。 Eventually, when the GC is called, this Unity.Object will be destroyed too. 最终,当调用GC时,该Unity.Object也将被销毁。

And this is why you should never create an Unity object which inherits directly from Unity.Object , but only from MonoBehaviour or ScriptableObject . 这就是为什么永远不要创建直接继承自Unity.Object ,而只能继承自MonoBehaviourScriptableObject的Unity对象的原因。

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

相关问题 我可以将未知对象转换为接口,以便可以在C#中访问其属性吗? - Can I cast an unknown object to an interface so I can access its properties in C#? 虽然我可以在方法中更改自定义 object 的属性,但我无法更改 Vector3(Unity,C#)的属性 - Although I can change properties of my custom object in methods, I cannot change properties of Vector3 (Unity, C#) 如果Object的所有属性在C#中为null,则将其设置为null - Set Object null if all its properties are null in C# 我如何在C#中访问返回对象的属性 - How can i access the properties of a returned object in c# 访问有关统一游戏对象的自定义属性 - Access custom properties about unity game object 在 Unity 中,我可以在 Inspector 窗口中公开 C# *Properties* 吗? - In Unity, can I expose C# *Properties* in the Inspector Window? 如何在 C# 中将 JSON 转换为自定义对象类并访问属性 - How to convert JSON to custom object class in C# and access properties C# 对象可以为“null”但仍具有属性吗? - Can a C# object be 'null' but still have properties? 我似乎无法在此 c# Unity 脚本中测试 null - I can't seem to test for a null in this c# Unity script C# 如何访问实现接口中不存在的 object 属性 - C# how can I access the object properties that not exist in the implemented interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM