How to check if an object with certain tag(or any of an array variable) is visible?

Hi.

I’ve been wondering how to check what it says in the title. I know that you can do “if(renderer.isVisible)”, but how can I do it with all objects of certain tag, so I don’t have to assign a variable for each of them. I could also do a “GameObject” array variable, and then check for every object of this certain tag in the scene, but I don’t know how to do it so they don’t have to be all in the view at a time.

Thanks,

Luka.

There are a number of different possibilities, but possibly the easiest/most efficient is to have a global function that adds to a list when an object becomes visible and removes from the list when it becomes invisible.

function RegisterVisible (go : GameObject) {
	if (go.CompareTag (checkTag)) {
		visibleList.Add (go);
	}
}

function UnregisterVisible (go : GameObject) {
	if (go.CompareTag (checkTag)) {
		visibleList.Remove (go);
	}
}

Then for each object, it can have a script that calls RegisterVisible (gameObject) in OnBecameVisible, and UnregisterVisible (gameObject) in OnBecameInvisible. That way, visibleList maintains a list of all visible objects with checkTag.

I’m assuming what you need is GameObject.FindGameObjectsWithTag (string tag);

This function returns an array with all the gameobjects in your scene with the given tag. That way you don’t need to assign every object to check.

Cheers