while() loops on a GameObject question

Hey if I have a while() loop on a GameObject that only terminates if the GameObject ceases to exist, what would the condition be? while(gameObject)? Would I get an error if I used while(1), because the loop would keep trying to do stuff to a game object which no longer exists?

A loop that terminates when the object ceases to exist??

I think you already have this:

void Update(){}

I don’t want it called every frame. And that doesn’t answer my question…

Better to have it called once every frame than as fast as possible (and thread blocking) in a while loop.

EDIT: likely your best bet is whatever script you have destroying the target GameObject, you instead call a different method first:

target.OnBeforeDestroy();
Destroy(target)

It won’t be looping as fast as possible, I’ll be using yield WaitForSeconds(). Also what you said might work but thats still not what I need to know…

while(true)
{
if (targetGameObject == null)
{
break;
}
}

I still strongly recommend against doing something like this, but there you go. (as an aside, I’m not sure if the targetGameObject will be set to “null” if it’s destroyed, guess you’ll find out!)