- Home /
Instantiate is spawning too many clones
Just at a bit of an impasse with this one if anyone can help.
I am developing an endless runner, and I am trying to instantiate my obstacle prefabs by having my player go through a trigger which activates an instantiation. The problem I'm having is that when the player goes through the trigger, my code seems to instantiate multiple prefabs at a time. I'll post screenshots and my codes to illustrate this better. The first screenshot shows the player going through its first trigger spawning 3 clones, and then the second trigger instantiating even more clones.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObsGenerator : MonoBehaviour
{
public GameObject spawnTrigger;
public GameObject Player;
[SerializeField] private List<Transform> obstacleList;
public Transform spawnLocation;
public void OnTriggerEnter2D(Collider2D Player)
{
if (Player.CompareTag("Respawn"))
{
// for (int i = 0; i < obstacleList.Count; i++)
{
spawnObstacle();
}
}
}
public void spawnObstacle()
{
for (int i = 0; i < obstacleList.Count; i++)
if (obstacleList.Count > 0)
{
Transform chosenLevelPart = obstacleList[Random.Range(0, obstacleList.Count)];
Instantiate(chosenLevelPart);
}
}
}
Really appreciate anybody's help!
Answer by Larry-Dietz · Dec 09, 2019 at 02:06 PM
You are telling it to spawn an obstacle for every obstacle in your list. What was your reasoning for putting that loop in the spawn code?
Get rid of the for loop, an it should only spawn a single random obstacle from your list each time the player enters the trigger.
Hope this help, -Larry
Hi Larry, it’s so obvious now you have said it. I’ve removed the for loop, I’m not quite sure why it’s there, I may have been experimenting and just missed it. Thanks for your comment!
Answer by pantang · Dec 09, 2019 at 01:54 PM
Its cheap, and if the player stays in the collider it will spawn more but this will give a 1 second delay. Im new and haven't done much with 2d colliders but I figure onenter for 2d is like on stay for 3d, at a guess.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObsGenerator : MonoBehaviour
{
public GameObject spawnTrigger;
public GameObject Player;
[SerializeField] private List<Transform> obstacleList;
public Transform spawnLocation;
float lastTime;
public void OnTriggerEnter2D(Collider2D Player)
{
if (Player.CompareTag("Respawn") && Time.time - lastTime > 1.0f)
{
// for (int i = 0; i < obstacleList.Count; i++)
{
spawnObstacle();
}
}
}
public void spawnObstacle()
{
for (int i = 0; i < obstacleList.Count; i++)
if (obstacleList.Count > 0)
{
Transform chosenLevelPart = obstacleList[Random.Range(0, obstacleList.Count)];
Instantiate(chosenLevelPart);
}
}
}
Your answer
Follow this Question
Related Questions
spawn from pool only if pool has inactive gameobjects 3 Answers
How do I rotate a prefab and instantiate it to go forward of my player? 0 Answers
i attach the prefab, but when i hit play, its not the prefab anymore 0 Answers
How to change Image on button click in a prefab that is instantiated 1 Answer
Call a function in an instanciated prefab has no effect on the prefab 2 Answers