- Home /
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;
}
But, it you get them to disappear after a while, won't that automatically solve spawning on top of each other? (Since you'll only have one at a time?) Plus, AddForce moves them, so won't that automatically make then not overlap?
$$anonymous$$ost people use overlapSphere to make sure the area is clear. But in your case, just remember if the last one was 1, 2 or 3, and don't roll that number for the next one.
Well, first of all, thank you for catching my addforce mistake. $$anonymous$$y objects werent moving so removing those lines of code didnt really alter my outcome.
As for having them disappear after a while, there is an issue with this. The game object used to create clones to spawn would disappear as well. I tried using some "estroy gameObject after x time" scripts but didnt have much success.
And its interesting what you said about not rolling the last number the next time. How can i dictate the code to do that? Even if a told it not to roll for the next one, i still need it to respawn there if the redcard gets destroyed. I changed the time it takes to spawn to 10 seconds which are helping my odds since a game session lasts a $$anonymous$$ute.
Thanks again for your comment. Im currently experimenting with limiting spawns with instantiate.
Destroying the "master" object by mistake is a whole different thing. The suggested way is to use prefabs (which you can still try to destroy by mistake.)
When I try to solve a lot of little problems at once, I can't tell which problem caused what -- easier to simplify and fix one things at a time.
Answer by smoggach · May 30, 2014 at 04:00 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150233im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
overlapping hear a song 1 Answer
OverlapCircleAll vs BoxCollider2D performance 0 Answers
overlapping UV on mobile 1 Answer
How to Destroy Terrains (destroy(this.gameobject) not working) 1 Answer