Home > Software design >  Removing a game object from the hierarchy-window
Removing a game object from the hierarchy-window

Time:10-21

So I have the problem that a lot of GameObjects are being created on launch and that it is cluttering the hierarchy window. My question is: Is there a way to remove those objects from the hierarchy window using code?

Destroy(); Doesn't achieve that it only removes all the components from a GameObject the GameObject itself preserves in the hierarchy window.

Here the code used:

private void DrawIndex()
    {
        if (xList != null)
        {
            foreach (var lis in xList) // This is the issue
            {                          //
                Destroy(lis);          //
            }
            
            Array.Clear(xList, 0, xList.Length);
            Debug.Log("Cleared Array!");
        }
        
        int xIndex = (int) Mathf.Floor(xLength);
        int yIndex = (int) Mathf.Floor(yLength);

        // Irrelevant part
        xList = new LineRenderer[xIndex];
        for (int i = 0; i < xIndex; i  )
        {
            xList[i] = InitLine(); // Not finished!
            xList[i].name = "X Index";
        }
    }

CodePudding user response:

Your issue is that

foreach (var lis in xList)
{                           
    Destroy(lis);          
}

is indeed only destroying the LineRenderer components themselves.

What you want to rather do is

foreach (var lis in xList)
{                          
    Destroy(lis.gameObject);         
}

in order to actually destroy the according GameObjects holding these LineRenderer components.


The

Array.Clear(xList, 0, xList.Length);

is pretty much useless overhead tbh

  • Related