- Home /
How to stop list from getting shorter ? [Beginner]
I'm currently making a 2D endless runner game in which the player stays still while terrain moves.
I've tried creating a pooling system that has a little problem :
When I run the game, the list which contains my prefabs (10 elements) keeps getting shorter. It continues until it reaches 0. An "Out of Range" message appears in the console. Obviously, I'm counting on my list to make this pooling system work.
Here is my code (very amateurish) :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Generator : MonoBehaviour {
public List<GameObject> platformList = new List<GameObject>();
//GameObjects
public GameObject newPlatform = null;
public GameObject transPlatform = null;
//Booleans
bool spawnNow = true; //Check if trans platform is gone
public bool triggerHit = false; //When this is true, allow spawn new prefab
//Numbers
int randomInt; //Random pick
float gameSpeed = -5f;
//Vector3
Vector3 offset = new Vector3(25,0,0);
void OnTriggerEnter2D(Collider2D other) {
//The "platforms" tag is the transplatform in this context
if (other.tag == "Platforms") {
triggerHit = true;
}
}
void Spawn () {
if (spawnNow == true) {
//Pick Random int
randomInt = Random.Range(1,9);
//Remove designated terrain from list/pool
platformList.RemoveAt(randomInt);
//Assign designated terrain to new platform variable
newPlatform = platformList[randomInt];
newPlatform.SetActive(true);
//Set position so it sticks to current terrain
newPlatform.transform.position = transPlatform.transform.position + offset;
//Set spawnNow to false
spawnNow = false;
}
}
void Despawn () {
while (triggerHit == true) {
if (spawnNow == false) {
//Add old platform back in to the list and deactivate
transPlatform.SetActive(false);
platformList.Add(transPlatform);
//Reset platform assignements
if (newPlatform.transform.position.x < 0.1f) {
transPlatform = newPlatform;
}
}
triggerHit = false;
spawnNow = true;
}
}
void SpawnAtStart () {
platformList.RemoveAt(0);
transPlatform = platformList[0];
transPlatform.SetActive(true);
transPlatform.transform.position = Vector3.zero;
}
// Use this for initialization
void Start () {
SpawnAtStart();
}
// Update is called once per frame
void Update () {
transPlatform.transform.Translate(gameSpeed * Time.deltaTime,0,0);
if (newPlatform != null){
newPlatform.transform.Translate(gameSpeed * Time.deltaTime,0,0);
}
Spawn();
Despawn();
}
}
Answer by rockyourteeth · Mar 19, 2014 at 09:00 PM
Not sure I fully understand the code, but it looks like you keep removing elements from the list... that is, of course, why your list is eventually empty.
You don't need to "remove" an item from the list in order to use it. Removing it just removes it forever.
Or maybe I'm just totally confused about how your code is working, I didn't read it that closely.
Looking at it further, it looks like you want to get a list item, we'll call it "itemA", but first you "remove" itemA. So itemA is no longer in existence in the list. So then you try to put itemA into your variable, it's not itemA, it's actually the next item, itemB. Then, when you despawn, you are putting itemB back in the list... but itemA is still gone forever. So each time you spawn, you lose one.
Try swapping lines 44 and 47. And also lines 88 and 90. This way, you will get a reference to that list item BEFORE you remove it.
It works ! Thank you very much for your help. Hopefully my coding skills will get better as I continue using Unity.
No problem, glad I could help. I was a noob myself not long ago. Please mark my answer as the correct one! ;)