- Home /
Object pulling prefabs that are randomly generating between random points?
Hello, originally i started using a generation script that is instantiating between a range of prefabs between an array of transforms. For a while it was working fine however i started using the profiler and realized this is causing a lot of problems with the cpu, it causing a lot of spikes when instantiating, and the trash manager building up. So i went into the object pooling live training video and created a basic object pooler. However, I'm still brain dead and mind boggled on how i could implement this strategy with the original script i have, and still keep it randomly spawning prefabs within the transform array. Any help or suggestions would be great. Here are the scripts.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Generation : MonoBehaviour {
// These are the spawn point arrays that are being used as placement for what's being spawned
public Transform[] terrainPoints;
public Transform[] obstaclePoints;
public Transform[] middlePoints;
// These are the actual list of prefabs that will be spawning
public GameObject[] terrainSpawn;
public GameObject[] obstacleSpawn;
public GameObject[] middleSpawn;
// These are the time intervals in which the spawnings will occur
public float terrainTimer = 2;
public float obstacleTimer = 2;
public float middleTimer = 2;
public float newTime = 2;
// This is the bool that determines whether the game will be pause (this is accessed by another script, not set by this one)
public bool isPause;
// This is accessing the level ring of the level, this isn't set in the inspector this is set dynamically
public GameObject theGround;
public static Generation S;
void Awake()
{
S = this;
theGround = GameObject.Find("level ring");
}
// Update is called once per frame
void Update () {
// If isPause is set to false then we proceed to spawn the clones using the custom functions
if(isPause == false)
{
TerrainSpawn();
ObstacleSpawn();
MiddleSpawn();
}
}
// This is the function thats being called thats doing the spawning for terrain prefabs
void TerrainSpawn()
{
// This is taking the duration of time and decreasing it by a set frame
terrainTimer -= Time.deltaTime;
// If this is below or equal to zero, it spawns a new clone
if(terrainTimer <= 0)
{
// This randomly chooses an indexed prefab and transform and spawns something at those points
GameObject terrainClone = Instantiate(terrainSpawn[Random.Range(0, terrainSpawn.Length)], terrainPoints[Random.Range(0, terrainPoints.Length)].transform.position, transform.rotation);
terrainClone.transform.parent = theGround.transform;
// This resets the duration of time for the next spawn
terrainTimer = newTime;
}
}
// This is the function called to spawn the obstacle clones
void ObstacleSpawn()
{
obstacleTimer -= Time.deltaTime;
if(obstacleTimer <= 0)
{
GameObject obstacleClone = Instantiate(obstacleSpawn[Random.Range(0, obstacleSpawn.Length)], obstaclePoints[Random.Range(0, obstaclePoints.Length)].transform.position, transform.rotation);
obstacleClone.transform.parent = theGround.transform;
obstacleTimer = newTime;
}
}
// This is the function being called to spawn clones for the middle section prefabs
void MiddleSpawn()
{
middleTimer -= Time.deltaTime;
if(middleTimer <= 0)
{
GameObject middleClone = Instantiate(middleSpawn[Random.Range(0, middleSpawn.Length)], middlePoints[Random.Range(0, middlePoints.Length)].transform.position, transform.rotation);
middleClone.transform.parent = theGround.transform;
middleTimer = newTime;
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ObjectPoolerScript : $$anonymous$$onoBehaviour {
public static ObjectPoolerScript current;
public GameObject pooledObject;
public int amountPooled = 20;
public bool willGrow = true;
List<GameObject> pooledObjects;
// Use this for initialization
void Start () {
pooledObjects = new List<GameObject>();
for(int i = 0; i < amountPooled; 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;
}
// Update is called once per frame
void Update () {
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612113652im_/https://answers.unity.com/themes/thub/images/avi.jpg)