是什么导致选择丢失activeGameObject?

问题描述

尝试在Unity 2019.3.14f中构建编辑器窗口时遇到以下错误

Assets\Editor\CellViewer.cs(19,40): error CS0117: 'Selection' does not contain a deFinition for 'activeGameObject'

我在统一答案板上发现一个人有此特定错误的证据,但5年内没有答案或更新。自此错误首次出现以来,我已经重新加载了项目,重新启动了unity,并多次重新启动了系统。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class CellViewer : EditorWindow
{
    private Map map;

    [MenuItem ("Window/Cell Viewer")]
    public static void ShowWindow() {
        EditorWindow.Getwindow(typeof(CellViewer));
    }

    void OnGUI () {
        
        if(Map.isinstanceActive())
        {
            GameObject obj = Selection.activeGameObject;
            Tile tile = obj.GetComponent<Tile>();
            if(tile != null) {
                map = Map.Instance();
                loadCell(map.getCell(tile.coordinates)));
            }
        }
    }
}

这怎么可能发生,我该怎么办?

解决方法

如果类名Selection含糊不清,可能会发生这种情况,因为您已经在全局名称空间中用该名称创建了一个类。

要确保您使用的是正确的代码,可以通过添加以下代码使代码明确: GameObject obj = UnityEditor.Selection.activeGameObject