- Home /
OnCollisionEnter2D() Instantiate Causing an Infinite Loop
Hey guys,
I'm trying to Instantiate a new object after an object leaves the spawning zone. Here's is what I've got so far (This is attached to the spawning zone GO):
void OnTriggerEnter2D(Collider2D col){
if(!outsideQuads.Contains(isHit)){
isInside = true;
}
}
void OnTriggerExit2D(Collider2D col){
isInside = false;
if(!outsideQuads.Contains(col.gameObject)){
outsideQuads.Add(col.gameObject);
}
}
void Spawner(){
Instantiate(quads[Random.Range(0, quads.Length)],
transform.position, Quaternion.identity);
}
And here is the script that's attached to the shapes:
void Start(){
dqs = GameObject.FindGameObjectWithTag("DS_Holder");
}
void OnCollisionEnter2D(){
if(!dqs.GetComponent<DisplayQuadScript>().isInside){
Instantiate(quads[Random.Range(0, quads.Length)],
transform.position, Quaternion.identity);
}
}
But this causing the game to spawn multiple objects, without waiting for the object inside of the spawning zone to exit. (Causing an Infinite Loop that crashes Unity, see pic below)
Any ideas?
I'm not sure exactly what how you are trying to achieve this, but try removing the ! operator from your OnTriggerEnter method.
Okay, so what I'm trying to do is this: If the spawn area is empty, and the current shape collides with anything, spawn a new shape.
Do the objects have that "DS_HOLDER" tag? if not it probably isn't finding any object and thinks the spawn is empty.
Your answer
Follow this Question
Related Questions
How Do I Add An Instantiated Object To An Array? 3 Answers
Too many instances 1 Answer
Instantiate 697283 trees by script? Crash! 2 Answers
Have a spawning issue on iOS 0 Answers
Invoke Ball Obstacle based on Score 0 Answers