Reputation: 50
I am struggling to identify the issue with the GameObject.Destroy(destroyCell) function on a Prefab item held in a list. 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. The list count shows the object is removed but Destroy is not taking effect.
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.
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.
Upvotes: 2
Views: 4185
Reputation: 90649
By calling Destroy
(which btw is in general in the UnityEngine.Object
class)
Object.Destroy(destroyCell);
You only destroy the Cell
component itself, not the according GameObject
!
Removes a GameObject, component or asset.
...
If
obj
is aComponent
, this method removes the component from theGameObject
and destroys it. Ifobj
is aGameObject
, it destroys theGameObject
, all its components and all transform children of theGameObject
.
It should rather be
Object.Destroy(destroyCell.gameObject)
Upvotes: 6