Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fred_1996 · Mar 19, 2014 at 08:51 PM · 2dlistrandomendless runnerpool

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();
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rockyourteeth · Mar 19, 2014 at 09:11 PM 0
Share

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.

avatar image Fred_1996 · Mar 19, 2014 at 09:53 PM 0
Share

It works ! Thank you very much for your help. Hopefully my coding skills will get better as I continue using Unity.

avatar image rockyourteeth · Mar 19, 2014 at 10:05 PM 0
Share

No problem, glad I could help. I was a noob myself not long ago. Please mark my answer as the correct one! ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

General pooling question 1 Answer

How do I Instantiate a prefab in a randomly generated location specific to tile type (2D Procedural game) 1 Answer

A node in a childnode? 1 Answer

Choose random prefabs from list? 1 Answer

Nodes are deleted from the list for no reason 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges