- Home /
finding clones and destroying them doesn't work
I am instantiating objects when a variable changes, but before I instantiate the object I find all clones and destroy them. My code looks like this:
void Update() {
GameObject[] Clones = GameObject.FindGameObjectsWithTag("clone");
if (Unlocked == true)
{
Unlocked = false;
Debug.Log(Clones.Length);
foreach (GameObject Clone in Clones)
{
Destroy(Clone);
}
UnlockPanel.tag = "clone";
foreach (Unlocks Unlock in UnlocksList)
{
if (Unlock.TargetGenerator == "Infrastructure")
{
GameObject Clone = Instantiate(UnlockPanel, InfrastructureContent);
var LevelText = Clone.transform.Find("UnlockLevelText");
LevelText.GetComponent<Text>().text = Unlock.TargetLevel.ToString();
var MultiplierText = Clone.transform.Find("MultiplierText");
if (Unlock.IsProfit == true)
{
MultiplierText.GetComponent<Text>().text = "Profits " + Unlock.MultiplierNumber + "x";
}
else
{
MultiplierText.GetComponent<Text>().text = "Profit speed " + Unlock.MultiplierNumber + "x";
}
if (Unlock.IsUnlocked == true)
{
Debug.Log("Yes");
Transform UnlockedOverlay = Clone.transform.Find("Unlocked");
UnlockedOverlay.gameObject.SetActive(true);
}
}}}
The parent to where the object is being instantiated is a child of a child. Everything works apart from the part where the existing clones are being destroyed. As you can see I have put a debug.log for the array length, but that only returns zero all the time. have already a script that works the same and it works perfectly there, that script looks like this:
void Update() {
GameObject[] Clones = GameObject.FindGameObjectsWithTag("upgradeclone");
if (_UpgradesList.Count != UpgradesList.Count)
{
_UpgradesList.Clear();
foreach (Upgrades _upgrades in UpgradesList)
{
_UpgradesList.Add(_upgrades);
}
foreach (GameObject Clone in Clones)
{
Destroy(Clone);
}
UpgradeButton.tag = "upgradeclone";
for (int n = 0; n < 5; n++)
{
GameObject Clone = Instantiate(UpgradeButton, ParentObject);
Clone.GetComponent<UpgradeScript>().InternalListItem = n;
}
}
}
Can someone please help me find the error.
Have you checked the clones before they are destroyed to make sure the tag is still applied to them?
Yes I checked every time I changed the variable if the clones still have the tag and they did.
Hmm in that case your code looks fine... I would do some experimenting with the FindGameObjectsWithTag() function, see if you can get it to find other tags there (maybe add a gameobject with tag = "test" and see if it picks that up) I'm not super familiar with the function personally, and would probably have to see more of the project to figure out whats wrong.
As a side note you probably dont want to destroy assets as the you dont have control of when the GC kicks in and it can cause performance issues. I would ins$$anonymous$$d cache your assets in a spawn pool and just return them to the spawn pool once done
Here is a semi-functional generic implementation you can use as reference.
Is there any easier way to do that because my second script works perfectly fine.
since this does seem to cause some issues i'd suggest to go down a completly different road and to Not use FindGameObjects...
since this is also always performance hungry...
you mention that all of your objects are in a hierarchy? why not get all Components from the hierarchy with FindObjectofType<yourTypeGoesHere>
?
also consider listening to @sacredgeometry . try to avoid unnecessary destruction and instantiation of objects since it is really performancehungry itself already and creates a lot of garbage... Performance does not grow on trees unfortunatly so choosing the "easier" way should not be the way to go.