I want to find any object with a name by button press…and if I find even one, I want it to keep looping until none are found…then I want to start a function.
GameObject current;
do
{
current = GameObject.Find("alumMaterial(Clone)");
if (current != null)
{
GameObject.Destroy(current);
}
} while (current != null);
// finished... you can call your method here
On button push I want a loop looking for an object with a specific name …if it is finding any…I don’t want anything to happen…when the last object is gone, then I want to call the function.
So you want it to find them all, do nothing with them & then do something? If it isn’t doing anything with them can’t you just do the search for them & if the answer is null then do all your other stuff?
Or as they are created add them to a list (& remove them from the list when they are destroyed) & do a count on the list instead, doing whatever you want whenever the count is 0? Be careful with the do/while loop as it can get stuck in it e.g. If the find/count is 0 & you don’t spawn any more of those items it will just keep doing your resets etc
in an Update function, anywhere. I’d recommend using a timer so it only checks like a few times a second or something though, or better yet making a manager that keeps all of those objects in a list and then have it run a function when the list count is zero. GameObject.Find() is extremely heavy in larger projects- you shouldn’t run it every frame.
On button press. An object is created. Every time the button is pressed another is created.
If another button is pressed I want to check to see if the objects that are created are still there or have been destroyed…but once the button is pressed all objects will be destroyed in a matter of seconds by another function…so I want the button press to keep running a loop to check until all the objects are gone then call the function.
There’s absolutely no reason for that code to crash your Unity- so you’re doing something else wrong. If you want help in figuring out what’s crashing your program, you need to post the code your using. Note that I didn’t use a loop, I used a conditional, because a loop is unnecessary for this part.
Update is a loop, every frame. You want to check if there are any objects this frame. If there is at least one, don’t do anything. If there are none, do something. You DO NOT need to put a loop in manually for that.
May not need to gate it in Update or whatever with the bool, didn’t really think that through… This is a decent pattern though. I think you get the idea.
For example I want to time trucks on the assembly line. I want the time that the trucks are started to finish. When the last truck is started and I decide to hit the stop button…well that’s not an accurate time because the last truck hasn’t finished yet.
If the stop button is pressed I want to wait for the last truck to finish before recording the time.