How to stop multiple overlapping spawns at one specific position?

I am making a 2D football themed game and I have currently set up 3 spawn points for a gameobject, a redcard. Whenever the football hits the randomly spawned redcard, it contributes -3 to my score. However, based on the code i have, multiple redcards spawn at a single spawn point and they spawn on top of each other. so when the ball hits that spawn point, all the overlapping red cards activate and my score plummets. I only want ONE red card at a specific spawn point. And i also would like for the red card to disappear after a certain amount of time after its spawned. I am very new to game development so your help is really appreciated. Thanks in advance. Here is my code. Redcard is the prefab. All of this works so far. its just the rest i need help with.

var timer : float = 0.0;
var spawning : boolean = false;
var prefab : Rigidbody;
var spawn1 : Transform;
var spawn2 : Transform;
var spawn3 : 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(1,4));
 
var location : Transform;
 
if(randomPick == 1){
  location = spawn1;
  Debug.Log("Chose pos 1");
 }
 else if(randomPick == 2){
  location = spawn2;
  Debug.Log("Chose pos 2");
 }
 else if(randomPick == 3){
  location = spawn3;
  Debug.Log("Chose pos 3");
 }
 
 
yield WaitForSeconds(1);
 
 spawning = false;
}

A good strategy for solving these kinds of problems is to break it up into smaller problems and dependencies. Let’s look at what is static about your scene:

  • You have 3 spawn points.
  • Your red cards all act the same way.
  • Some logic is deciding whether it’s time to spawn a card or not.

So let’s find a good angle to look at this problem. Spawn points depend on the card it contains. Cards depend on some logic to decide whether they should spawn or not. At the highest level there is something deciding when to spawn a card. So let’s start there.

  1. Create an object that does nothing but decide when to spawn a card.
  2. Create a red card object that does nothing but spawn at a given point, do it’s thing, then die. (or react when the user triggers it).
  3. Create a spawn point object that does nothing but instantiate a red card object if all the conditions are met to do so then destroy it when necessary.

Now you have everything you need. Unity makes it incredibly easy to solve problems this way.