Check if a spawn point it occupied

I have a script which is spawning pickups at random spawn points.

Right now I will end up with multiple pickups at each point and they will continue to spawn.

How would I go about checking if a spawn point is occupied?

I am working in Java script I will include my spawn code below.

var timer : float = 0.0;
var spawning : boolean = false;
var prefab : GameObject;
var spawn : Transform[];

 function Update () {
 
 if(!spawning){
  timer += Time.deltaTime;
 }

 if(timer >= 2){
  Spawn();
 }
}
 
function Spawn(){
 spawning = true;
 timer = 0;
 

var randomPick : int = Mathf.Abs(Random.Range(0,3));
 
var makePick : GameObject = Instantiate(prefab, spawn[randomPick].position, spawn[randomPick].rotation);
 

 yield WaitForSeconds(1);
 spawning = false;
}

2 Answers

2

Here is a first idea:

var spawnPoint:Vector3 = spawn[randomPick].position;
var hitColliders = Physics.OverlapSphere(spawnPoint, 2);//2 is purely chosen arbitrarly
if(hitColliders.Length > 0) //You have someone with a collider here

You would have to make sure the spawn point is high enough not to get the ground for instance. Or you could use some if statement to remove some of the objects but I would guess that should get you started.

Thanks would you add this to it's own function or add it within the spawn function?

never mind I was being daft that's what happens when artists try to code lol. This seems to be working perfectly I will post my code in case anybody else needs to see how it is used. Thanks very much for your help.

This is how I implemented fafase suggestion into my pickup code

var timer : float = 0.0;
var spawning : boolean = false;
var prefab : GameObject;
var spawn : Transform[];

//check if spawning then when timer is down to 2 seconds use spawn function
 function Update () 
 
 {
 	if(!spawning){
	timer += Time.deltaTime;
	}
		if(timer >= 2){
		Spawn();
	}
}
 
function Spawn(){

 //Stop timer and set to 0
	spawning = true;
	timer = 0;
 		
Debug.Log ("Spawn Set to True");
 


var randomPick : int = Mathf.Abs(Random.Range(0,3));
var spawnPoint:Vector3 = spawn[randomPick].position;
var hitColliders = Physics.OverlapSphere(spawnPoint, 0.1);


	if(hitColliders.Length > 0.1) //You have someone with a collider here

	
//halt script for 1 second set spawn to false then return to the start of the process
 {
 	yield WaitForSeconds(1);
 	spawning = false;
 	
Debug.Log ("Spawn Occupied");

 }
	else
 {
 
 //create the object at point of the spawn variable 
 var thingToMake : GameObject = Instantiate(prefab, spawn[randomPick].position, spawn[randomPick].rotation);
 
 Debug.Log ("Checked and spawning");
 
 
//halt script for 1 second set spawn to false then return to the start of the process
 	yield WaitForSeconds(1);
 	spawning = false;
 
Debug.Log ("Pickup spawned restarting");
	}
}