Which kind of loop?:

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.

I have this in my button function:

 while (GameObject.Find("alumMaterial(Clone)") == null)
            {
                startedTimer = false;
                stopTimerButtonenabled = false;
                stopwatch.Stop();
                timeValue.text = stopWatchvalue.text;
                stopwatch.Reset();
            }
GameObject current;
do
{
  current = GameObject.Find("alumMaterial(Clone)");
  if (current != null)
  {
      GameObject.Destroy(current);
  }
} while (current != null);

// finished... you can call your method here

hope this is what you are looking for!?

That crashed unity.

Maybe I am not going about this the right way.

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.

do you mean “I only want this function to run when the user pushes button x and there are none of y in the scene”?

if so it’s just

GameObject current;
current = GameObject.Find("alumMaterial(Clone)")
if(checkButton() && !current)
{
// do stuff
}

I’m using checkbutton because I don’t know if that’s keyboard button or ui button :slight_smile: you’ll need to provide a boolean return function there.

The “keep looping” is the normal game loop, frame after frame after frame

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

put:

if(GameObject.Find("SomeObjectName") == null)
{
   someFunction();
}

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.

I’ve tried that with the code above but it crashes it.

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.

That condition works…but only once. Sorry…I thought it was a loop for a second.

I need it to continually check until the objects are destroyed. Not only once.

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.

        private bool killingIt;
        private static void KillGo()
        {
            while (true)
            {
                GameObject go = GameObject.Find("killThis");
                if (go != null)
                {
                    Destroy(go);
                    continue;
                }
                break;
            }
        }

        void Update()
        {
            if (Input.GetButtonDown("someButton") && !killingIt)
            {
                killingIt = true;
                KillGo();
                killingIt = false;
            }
        }

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.

Is that what you were looking for?

Put the objects in a list & just do an if statement in update that checks to see if the list count is 0

I need this function to happen when a button is pressed…not before.

That doesn’t work…I’d have to hit the button only after all the objects are destroyed to work.

No, this destroys everything with that name when you press the button. If there’s nothing to destroy, it stops.

I don’t want to destroy them…I want to wait until they are destroyed by the other function.

I really don’t understand why everyone is obsessing about the destruction.

Do something when none of object “x” exists and a button has been pushed. <=== that is the entire design as AA has described it.

1 Like

I’m really not sure what purpose it serves to collect them, then.

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.