简体   繁体   English

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

[英]Unity GameObject.Destroy() not working with direct reference

I am struggling to identify the issue with the GameObject.Destroy(destroyCell) function on a Prefab item held in a list.我正在努力确定列表中保存的预制项目上的 GameObject.Destroy(destroyCell) function 的问题。 I have shown there is a direct reference to the object by calling destroyCell.Select() {internal function to highlight the Cell object} on it before calling Destroy and removing it from the list.在调用 Destroy 并将其从列表中删除之前,我已经通过调用 destroyCell.Select() {internal function 以突出显示 Cell 对象} 来显示对 object 的直接引用。 The list count shows the object is removed but Destroy is not taking effect.列表计数显示 object 已删除,但 Destroy 未生效。

The general idea is to click and drag from a position with cells being added to a container object relative to dragged distance from the origin, and removing the cells as you move back to the origin.一般的想法是从 position 中单击并拖动,单元格被添加到容器 object 相对于原点的拖动距离,并在您移回原点时移除单元格。

The mechanism for adding and removing cells is shown below:添加和删除单元格的机制如下图所示:

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);
    }

Any pointers would be greatly appreciated.任何指针将不胜感激。

By calling Destroy (which btw is in general in the UnityEngine.Object class)通过调用Destroy (顺便说一句,通常在UnityEngine.Object类中)

Object.Destroy(destroyCell);

You only destroy the Cell component itself, not the according GameObject !只会破坏Cell组件本身,而不是相应GameObject

Removes a GameObject, component or asset.移除游戏对象、组件或资产。

... ...

If obj is a Component , this method removes the component from the GameObject and destroys it .如果objComponent ,此方法会从GameObject中删除该组件并销毁它 If obj is a GameObject , it destroys the GameObject , all its components and all transform children of the GameObject .如果objGameObject ,它会销毁GameObject 、它的所有组件以及GameObject的所有变换子对象。

It should rather be应该是

Object.Destroy(destroyCell.gameObject)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM