❔ Need to delete all the Cloned Prefabs
how would I fix this code so it deletes all cloned fish prefabs if their Float(thisLength) is smaller than the current spawned one?
I have no idea if any of this is useless or not because I'm grasping at straws. if you want more info you will need to ask specifics cos I'm dumb and have been working for 2 days straight XD
2 Replies
Taking a look at your code many questions arise.
1) Why do you call
fishPrefab.GetComponent<FishController>()
in the beginning?
2) Why do you increase the fishAmount
eventhough you apparently don't Instantiate a fish or do something similar
3) What do you try to accomplish with this line FishController fishController = fishPrefab.GetComponent<FishController>()
you never use fishController
anywhere in this scope?
4) What is FishController
, is it a static class?
5) What should the function do in the first place the name StoreFishy()
indicates that you want to store a Fish(y) in some way but there aren't even signs that something like that happens anywhere? It seems more like you are trying to create and add additional fish to the scene.
As a tip: Do not post screenshots of your code instead put it into a code environment (start with three ` (backticks) and end with three `)
------------------------------------------------------------------------------
To actually answer your question:
To delete all shorter fishes you at first have to get access to all fish, check if they are shorter and if they are delete them (Unity calls it Destroy).
How to access all fish:
Option 1: Keep track of the fish you've already created in a List somewhere and remove them when there are destroyed.
Option 2: If all fish share a parent you can call GetComponentsInChildren<FishController>()
on this parent and you should get a List of FishControllers which enables you to access the fish as well. (I wouldn't recommend this option because it is more expensive to execute and in my opinion more complicated to implement).
How to check if they are shorter:
If you have a list of all fish iterate over all of them and compare their lengths and delete them accordingly.
To other people reading this:
Do not tell me that this is not the optimal way of implementing this. Yes, there are ways to speed it up like storing the fish sorted by length and looking up the fish length boundary with Binary Search. However, I don't think this is helpful advise in the moment.Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.