Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EditorGUILayout.ObjectField未接收引用_C#_Unity3d - Fatal编程技术网

C# EditorGUILayout.ObjectField未接收引用

C# EditorGUILayout.ObjectField未接收引用,c#,unity3d,C#,Unity3d,我正在学习自定义编辑器。此时会出现对象字段,可以将包含它们的目录脚本或游戏对象拖到插槽中。但是当你放弃的时候,什么都没有发生。该字段亮起,但掉落的对象不会粘住 当我按下按钮时,target为null public class AddInventory : EditorWindow { public Inventory target; [MenuItem("Inventory/Add items")] public static void ShowWindow()

我正在学习自定义编辑器。此时会出现
对象字段
,可以将包含它们的
目录
脚本或
游戏对象
拖到插槽中。但是当你放弃的时候,什么都没有发生。该字段亮起,但掉落的对象不会粘住

当我按下按钮时,
target
null

public class AddInventory : EditorWindow {

    public Inventory target;

    [MenuItem("Inventory/Add items")]
    public static void ShowWindow()
    {
        GetWindow<AddInventory>("Add items");
    }

     void OnGUI()
    {
        GUILayout.Label("Add items to  inventory", EditorStyles.boldLabel);
        Inventory target = null;
        target = (Inventory) EditorGUILayout.ObjectField("Inventory thingy", target, typeof(Inventory), true);


        if (GUILayout.Button("I am button!"))
        {
            Debug.Log(target.thing);

        }
    }
}

您正在重新定义目标。删除
库存目标=nullObjectField
时,请确保
Inventory
类当然是一个统一的
对象

public class AddInventory : EditorWindow {

    public Inventory target;

    [MenuItem("Inventory/Add items")]
    public static void ShowWindow()
    {
        GetWindow<AddInventory>("Add items");
    }

     void OnGUI()
    {
        GUILayout.Label("Add items to  inventory", EditorStyles.boldLabel);

        target = (Inventory)EditorGUILayout.ObjectField("Inventory thingy", target, typeof(Inventory));   

        if (GUILayout.Button("I am button!"))
        {
            Debug.Log(target.thing);    
        }
    }
}
公共类附加项:编辑器窗口{
公共库存目标;
[菜单项(“库存/添加项目”)]
公共静态void ShowWindow()
{
获取窗口(“添加项目”);
}
void OnGUI()
{
Label(“将项目添加到库存”,editorstyle.boldLabel);
target=(Inventory)EditorGUILayout.ObjectField(“Inventory thingy”,target,typeof(Inventory));
if(GUILayout.Button(“我是Button!”)
{
Debug.Log(target.thing);
}
}
}

@Lautaro确实如此,但是在调用
ObjectField
之前,您将
target
设置为
null
,因此
target
将始终为
null
。你有没有按照建议去掉那条线?这是我第一次尝试!但它在你发布新答案前几分钟起了作用。这当然是最有意义的。谢谢
public class AddInventory : EditorWindow {

    public Inventory target;

    [MenuItem("Inventory/Add items")]
    public static void ShowWindow()
    {
        GetWindow<AddInventory>("Add items");
    }

     void OnGUI()
    {
        GUILayout.Label("Add items to  inventory", EditorStyles.boldLabel);

        target = (Inventory)EditorGUILayout.ObjectField("Inventory thingy", target, typeof(Inventory));   

        if (GUILayout.Button("I am button!"))
        {
            Debug.Log(target.thing);    
        }
    }
}