- Home /
Destroy(Gameobject) not working
My code is not working. here is my code and I will explain what it outputs
private void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
{
for (int i = 0; i < yNum; i++)
{
for (int j = 0; j < xNum; j++)
{
for (int k = 0; k < zNum; k++)
{
if (row[i * (xNum * zNum) + j * zNum + k].GetComponent<Renderer>().material.color.Equals(userRow[i * (xNum * zNum) + j * zNum + k].GetComponent<Renderer>().material.color))
{
alltrue[i * (xNum * zNum) + j * zNum + k] = true;
}
else
{
alltrue[i * (xNum * zNum) + j * zNum + k] = false;
}
}
}
}
if (alltrue.ToList<bool>().All(b => b))
{
Debug.Log("Good");
b.LevelUp();
level++;
int buildSize = GameObject.FindGameObjectsWithTag("build").Length;
int displaySize = GameObject.FindGameObjectsWithTag("display").Length;
GameObject[] builds = GameObject.FindGameObjectsWithTag("build");
DestroyArray(builds);
GameObject[] displays = GameObject.FindGameObjectsWithTag("display");
DestroyArray(displays);
xNum++;
yNum++;
Debug.Log("build: " + buildSize + " || " + GameObject.FindGameObjectsWithTag("build").Length);
Debug.Log("display: " + displaySize + " || " + GameObject.FindGameObjectsWithTag("display").Length);
Generate();
}
else
{
Debug.Log("Not Good");
}
}
void DestroyArray(GameObject[] b)
{
foreach (GameObject k in b)
Destroy(k);
}
}
now as you can see I have many debug.logs. In this line Debug.Log("build: " + buildSize + " || " + GameObject.FindGameObjectsWithTag("build").Length); it logs: 12||12 it should log "12||0" because it was supposed to destroy all the objects in the array. Can somebody help me?
Answer by SirPaddow · Jun 08, 2019 at 08:27 PM
Check here: https://docs.unity3d.com/ScriptReference/Object.Destroy.html
"Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering."
So if you check during the next call to "Update", your log will show 0.
Your answer

Follow this Question
Related Questions
What is the best way to instatiate an array of connected GameObjects? 0 Answers
Can't Activate a GameObject from Array 1 Answer
Read different game objects into array at same time? 2 Answers
How to store references of generated objects 1 Answer
Order a GameObject array based on an int property of each object? 1 Answer