- Home /
The question is answered, right answer was accepted
Why am I spawning a ton of prefabs in one area?
I'm trying to create a really basic 3d runner type of game, and I'm trying to instantiate different prefabs that I have created to use as the floor. I'm trying to use a game object with an object pooler script to spawn a prefab that changes into one of the different prefabs that I'm trying to use as the floor. I'm trying to make it so that the spawner prefab/quad changes in its z position every time it is spawned again so that the floor will be spawned further as the player moves further. The problem is that the prefabs are being spawned in the same position and a ton of them are being instantiated. These are my two scripts:
Object Pooling:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ObjectPooler : MonoBehaviour {
public static ObjectPooler current;
public GameObject pooledObject;
public int pooledAmount = 10;
public bool willGrow = true;
List<GameObject> pooledObjects;
void Start()
{
pooledObjects = new List<GameObject> ();
for (int i = 0; i < pooledAmount; i++) {
GameObject obj = (GameObject)Instantiate (pooledObject);
obj.SetActive (false);
pooledObjects.Add (obj);
}
}
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (!pooledObjects [i].activeInHierarchy)
{
return pooledObjects [i];
}
}
if(willGrow)
{
GameObject obj = (GameObject)Instantiate (pooledObject);
pooledObjects.Add (obj);
return obj;
}
return null;
}
}
Spawning/Instantiating the Floor:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpawningPrefabs : MonoBehaviour {
List<GameObject> prefabList = new List<GameObject>();
public GameObject Prefab0;
public GameObject Prefab1;
public GameObject Prefab2;
public GameObject Prefab3;
public GameObject Prefab4;
public float previousZ;
void Start()
{
prefabList.Add (Prefab0);
prefabList.Add (Prefab1);
prefabList.Add (Prefab2);
prefabList.Add (Prefab3);
prefabList.Add (Prefab4);
}
void Update(){
CreatePrefab ();
}
void CreatePrefab ()
{
int prefabIndex = UnityEngine.Random.Range(0,5);
Instantiate(prefabList[prefabIndex], new Vector3(0.0f, 0.0f, previousZ + 10.0f), Quaternion.identity);
changeZ ();
}
void changeZ()
{
previousZ = GetComponent<Rigidbody>().transform.position.z;
}
}
Answer by Elisvaldo · Dec 28, 2017 at 05:32 PM
You are calling the CreatePrefab() in the Update() function. So it will instantiate one every frame!
Where should I put the CreatePrefab()? I tried putting it in the Start() function but that spawns one and nothing else happens.
That is a good question, and one where the fun of game development starts :) If you are making an endless runner, then I'd say you wan to instantiate sometime around when the player runs every X meters. Or maybe instantiate every time the player passes one obstacle. $$anonymous$$aybe you want to instantiate less obstacles if the player is running faster, and more obstacles when it is slower. $$anonymous$$aybe every Y meters you want to make a "hard section" and you instantiate more in there than in the other areas. You will have to figure out this one by yourself, as it depend a lot on what your game is all about!
Answer by S_jay1 · Dec 30, 2017 at 03:55 PM
Thank you for the response! Could you give me a really simple/basic code structure of what instantiating every Y meters would look like? It sounds like exactly what I need but I'm not sure how to do that.
something like:
var nextDistance = 5;
void Update()
{
if(player.transform.position.x > distance)
{
InstantiateObject();
nextDistance += player.transform.position.x;
}
}
Follow this Question
Related Questions
Saving customized transform in game 1 Answer
Why Transform and not Prefab? 2 Answers
transforming a prefab randomly at runtime 3 Answers