Objects in object pool aren't being made
I know this code is sort of messy but I've been so bugged today with object pooling just not working out at all. In an earlier script I can't get it to activate I've been trying to rewrite it in mutliple ways but this way doesn't even create the GameObject into the pool. Its not giving me any errors but its just not creating anything. I've already set the game objects for the GameObjects like rowOf1 and such through prefabs. Please check out my other question as well asking why a similar script won't work.
http://answers.unity3d.com/questions/1327128/code-wont-activate-items-from-object-pool.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RowSpawner : MonoBehaviour
{
public bool isAlive;
float xPos;
Vector3 rowSpot;
public int random;
public GameObject rowOf1;
public GameObject rowOf2;
public GameObject rowOf3;
public GameObject rowOf4;
public int pooledAmount;
public List<GameObject> row1;
public List<GameObject> row2;
public List<GameObject> row3;
public List<GameObject> row4;
void start ()
{
row1 = new List<GameObject> ();
row2 = new List<GameObject> ();
row3 = new List<GameObject> ();
row4 = new List<GameObject> ();
isAlive = true;
InvokeRepeating ("Spawner", 2.0F, 1.0F);
xPos = 0;
for (int i = 0; i < pooledAmount; i++) {
GameObject obj1 = (GameObject)Instantiate (rowOf1);
obj1.SetActive (false);
row1.Add (obj1);
GameObject obj2 = (GameObject)Instantiate (rowOf2);
obj2.SetActive (false);
row2.Add (obj2);
GameObject obj3 = (GameObject)Instantiate (rowOf3);
obj3.SetActive (false);
row3.Add (obj3);
GameObject obj4 = (GameObject)Instantiate (rowOf4);
obj4.SetActive (false);
row4.Add (obj4);
}
}
void update ()
{
}
void Spawner ()
{
random = Random.Range (1, 5);
xPos = xPos + 10F;
rowSpot = new Vector3 (xPos, 0.35F, 0);
if (isAlive == true) {
if (random == 1) {
for (int i = 0; i < row1.Count; i++) {
if (!row1 [i].activeInHierarchy) {
row1 [i].transform.position = rowSpot;
row1 [i].transform.rotation = Quaternion.identity;
row1 [i].SetActive (true);
break;
}
}
}
if (random == 2) {
for (int i = 0; i < row1.Count; i++) {
if (!row1 [i].activeInHierarchy) {
row1 [i].transform.position = rowSpot;
row1 [i].transform.rotation = Quaternion.identity;
row1 [i].SetActive (true);
break;
}
}
}
if (random == 3) {
for (int i = 0; i < row1.Count; i++) {
if (!row1 [i].activeInHierarchy) {
row1 [i].transform.position = rowSpot;
row1 [i].transform.rotation = Quaternion.identity;
row1 [i].SetActive (true);
break;
}
}
}
if (random == 4) {
for (int i = 0; i < row1.Count; i++) {
if (!row1 [i].activeInHierarchy) {
row1 [i].transform.position = rowSpot;
row1 [i].transform.rotation = Quaternion.identity;
row1 [i].SetActive (true);
break;
}
}
}
}
}
}
Although it isn't how I'd implement Object Pooling, that script should work. You said it doesn't create the objects at all?
Quick checks:
the object bearing this script is in the scene, active, and the script is enabled?
pooledAmount is not left to its default value (0) ?
Answer by Commoble · Mar 17, 2017 at 02:51 PM
Change start()
to Start()
. Capitalization matters. Same with Update.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113346im_/https://answers.unity.com/themes/thub/images/avi.jpg)