Hi I am new on Unity as well as C sharp. I wish to do a scrolling game where enemies, packs and bullets spawn randomly. I used a spawn script but I have drastical frame rates losses. I wish to generate a pooling system that can work with the Spawn script I used is that possible? And can I have a brief explanation of how a pooling system works? All the explanations I fell on were vague on their use.
Spawn script it works great but creates lags :
using UnityEngine;
using System.Collections;
public class spawn : MonoBehaviour
{
// Use this for initialization
public GameObject[] Prefabs;
public float numEnemies;
public float xLft = 19F;
public float xRgt = 85F;
public float yUp = 3.5F;
public float yDwn = -4.5F;
public float spawnCooldown = 1;
private float timeUntilSpawn = 0;
void Start()
{
//gm = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<GameMaster>();
}
// Update is called once per frame
void Update()
{
timeUntilSpawn -= Time.deltaTime;
if(timeUntilSpawn <= 0)
{
// Do your enemy spawns here
for (int i = 0; i < numEnemies; i++)
{
SpawnEnemy(); //Spawn Fxn called
}
// Reset for next spawn
timeUntilSpawn = spawnCooldown;
}
}
private void SpawnEnemy()
{
//Fxn that Spawns the enemy and determines its position, Trying to make enemy prefab being produced as a child gameObject.
GameObject enemyPrefab = Prefabs[Random.Range(0, Prefabs.Length)];
Vector3 newPos = new Vector3(Random.Range(xLft, xRgt), Random.Range(yUp, yDwn), 0);
GameObject action = Instantiate(enemyPrefab, newPos, Quaternion.identity) as GameObject;
action.transform.parent = transform;
}
}