Unity GameObject.Destroy() 不适用于直接引用

EAY*_*AJB 2 c# unity-game-engine

我正在努力找出列表中预制件上的 GameObject.Destroy(destroyCell) 函数的问题。我已经表明,在调用 Destroy 并将其从列表中删除之前,通过调用 destroyCell.Select() {内部函数以突出显示 Cell 对象},可以直接引用该对象。列表计数显示对象已被删除,但 Destroy 未生效。

总体思路是从相对于距原点拖动距离的单元格添加到容器对象的位置单击并拖动,并在移回原点时删除单元格。

添加和删​​除单元的机制如下所示:

if (mouseDistFromOrigin < prevDragInteractFloorValue && cellsToModify.Count > 1)
{
    // REMOVE CELL
    cellsAvailableToGenerate++;
    energyAvailable += CellHelper.EnergyGainPerCell;

    nextCellYOffset /= CellHelper.NewCellGrowthRatio;
    dragInteractFloorValue = prevDragInteractFloorValue;
    prevDragInteractFloorValue -= nextCellYOffset * cellsToModify[cellsToModify.Count - 1].transform.localScale.y;

    //var containerCells = container.GetComponentsInChildren<Cell>();
    //Debug.Log("CELLS " + containerCells.Length);
    //var destroyCell = containerCells[containerCells.Length - 1];
    //GameObject.Destroy(destroyCell);
    //GameObject.Destroy(containerCells[containerCells.Length - 1]);

    Debug.Log("CELLS: " + cellsToModify.Count);

    var destroyCell = cellsToModify[cellsToModify.Count - 1];
    destroyCell.Select();
    cellsToModify.Remove(destroyCell);
    GameObject.Destroy(destroyCell);

    Debug.Log("CELLS: " + cellsToModify.Count);
} else if (mouseDistFromOrigin > dragInteractFloorValue && 
    cellsAvailableToGenerate > 0 && 
    energyAvailable >= CellHelper.EnergyGainPerCell)
    {// CAN GENERATE CELL
        prevDragInteractFloorValue = dragInteractFloorValue;
        // ADD CELL
        Cell prev = cellsToModify[cellsToModify.Count - 1];
        Vector3 top = CellHelper.TopOfCellPos(prev);

        Vector3 pos = new Vector3(top.x, top.y, 0);
        Quaternion rot = Quaternion.Euler(0, 0, angle);

        Debug.Log("NEW - LENGTH " + mouseDistFromOrigin +
            " FLOOR " + dragInteractFloorValue +
            " ANGLE: " + Utils.ZeroAngleDeg(angle + angleOffset) +
            " X " + pos.x +
            " Y " + pos.y);

        Cell cell = Object.Instantiate(prev, pos, rot, container.transform);
        cell.transform.localScale = CellHelper.LocalScaleFromPreviousCell(prev);
        CellHelper.SetColourByColour(cell, cellFadeColour);

        // MODIFY VALUES
        cellsAvailableToGenerate--;
        energyAvailable -= CellHelper.EnergyGainPerCell;

        nextCellYOffset *= CellHelper.NewCellGrowthRatio;
        dragInteractFloorValue += nextCellYOffset * cell.transform.localScale.y;

        cellsToModify.Add(cell);
    }
Run Code Online (Sandbox Code Playgroud)

任何指示将不胜感激。

der*_*ugo 5

通过调用 Destroy(顺便说一句,这通常在UnityEngine.Object班级中)

Object.Destroy(destroyCell);
Run Code Online (Sandbox Code Playgroud)

只会破坏Cell组件本身,而不是相应的GameObject

删除游戏对象、组件或资产。

...

如果obj是 a Component,则此方法从 中删除该组件GameObject并销毁它。如果obj是 a GameObject,它会销毁GameObject、其所有组件以及 的所有变换子项GameObject

更应该是

Object.Destroy(destroyCell.gameObject)
Run Code Online (Sandbox Code Playgroud)