Checking for objects NOT in level

I have the following code in Update, on a spawner I’ve made:

function Update () {
//search for the ball physics object in the level
	var ballExists = GameObject.FindGameObjectsWithTag("Player");
	Debug.Log("variable ballExists is equal to " + ballExists);
	
	if(ballExists == null) {
		//press launch button to start spawning a ball
		//TO DO - Add control check code so space must be pressed before firing
		SpawnBall();
	}
}

Using the Debug Log, I have found that this never fires SpawnBall().

Being new to using Javascript, I don’t know if I am just using the result of the search wrong. All the examples in the manual never check if something DOESN’T exist, they iterate through objects that DO exist.

According to the manual, it’s supposed to be passing back a “null” for me if 0 of the objects exist. Instead, my debug line is giving me this:

“variable ballExists is equal to UnityEngine.GameObject[ ]”

I’m guessing because it is equal to UnityEngine.GameObject[ ] instead of null, the if statement never fires. I suppose I could get around this by checking if it returns “UnityEngine.GameObject[ ]”, but I would rather know how to properly read the answer Unity is passing back. :wink:

Is there something special I need to do with the stuff it’s passing back to me?

I’d actually recommend a different approach entirely, because doing FindGameObjectsWithTag every frame is not exactly fast. I don’t know what you’re doing exactly, but from what I can guess, what I’d do is have a global variable that increases by one every time a ball spawned, and decreases by one when a ball is destroyed. So if it’s ever 0, you know there aren’t any existing balls.

–Eric

I figured it out after taking a break. GameObject.FindGameObjectsWithTag returns an Array of the objects, and returns a blank array instead of a null when the objects can’t be found. So I just had to check for if the arrays .length was 0. :sweat_smile: So then, if GameObject does pass back Null, it probably means I’ve nuked the global GameObject somehow and seriously messed up my code. :stuck_out_tongue:

That global value thing does sound like it would be a better approach. I’ll have to figure out how to make a global value in Unity first though. I’m thinking it’s probably like private, ie:

Global Var numberOfBalls = 0;

But if that doesn’t work, I’ll have to pour over the manual again.

I’m really new to this method of scripting, as you can probably tell by my epic failure to understand what the documentation was trying to say above. I thought by GameObject not existing, it meant the objects I searched for. It never occured to me that the GameObject function is part of this actual GameObject entity existing in memory. :sweat_smile: :lol:

static var BallsInPlay = 0;

That’s how I make stuff global in Unity. Just for reference in case anyone else reads this later and needs to know. :slight_smile:

Ok, I’ve got everything checking a global variable attached to the StartSpawnpoint script. Since there will never be more then one start point in a level, and never will be a level without one, it’s a safe element to attach the global value to, I hope. (Now watch as my level designer comes up with something random that breaks these rules, and I scream. :wink: )


Got another question. I have a camera and a virtual ball that follows the position of the real physics ball(this is so that I can do collisions independant of what the real ball looks like, and swap the ball for other objects as it transforms.)

Currently their code looks like this:

function Update () {

if(StartSpawnpoint.BallsInPlay > 0){
	//set x and y to BallControlPoint postion
	var BallFollow = GameObject.FindWithTag("Player");
	var x = BallFollow.transform.position.x;
	var z = BallFollow.transform.position.z;
	var y = BallFollow.transform.position.y;

	transform.position.x = x;
	transform.position.z = z;
	transform.position.y = y;
	}

}

I’m trying to think how to write that without doing a search for the ball object, as you said to avoid doing that as much as possible. But as you can see, I need to get the position of the “Player” tagged object.

I thought of using global/static variables for the position of the ball, but then realized that any script checking for those global values is either going to:

1 - Have to check for the ball using a search anyway.

or

2 - Be attached to the ball, and thus deleted once the ball no longer exists. This kills any point to the optimization, because every object checking for the variables will need to check for the object existing anyway.

Is there a way to get the position of the object without doing a FindWithTag() function? Or is the FindWithTag function fast enough to not worry about this? I’m kind of confused how to do this stuff without actually getting (through searching for) the other objects.