Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DerperDoing · Mar 18, 2018 at 06:14 PM · listscountobject poolinfinite runner

Why only one element is displayed by count when the list has 5 elements in it?

I made a list of 5 GameObjects and they are being displayed in the hierarchy(inactive though) and the print statement tells that it has 5 elements.
But when I call the GetPlatform method from another class and print the count of the List, it is displaying only 1 element which thereby, is returning null after one iteration.
Why is it happening so?

 public class ObjectPooler : MonoBehaviour {
 
     public GameObject prefab;
     public int size;
     List<GameObject> plat;
 
     void Start () {
         plat = new List<GameObject> ();
         for (int i = 0; i < size; i++) {
             GameObject o = (GameObject)Instantiate(prefab);
             o.SetActive (false);
             plat.Add (o);
         }
         print (plat.Count);
     }
 
     public GameObject GetPlatform() {
         print (plat.Count);
         if(plat.Count>0)
         {
             GameObject temp= plat[0];
             plat.RemoveAt(0);
             print ("GetPlatform called");
 //            temp.SetActive(true);
             return temp;
         }
         return null;
     }
 
     public void DestroyPlatform(GameObject obj)
     {
         plat.Add (obj);
         obj.SetActive (false);
     }
 
 }
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

Answer by Lilius · Mar 18, 2018 at 06:30 PM

Its null because you remove the first object from the list and GetPlatform method tries to access first object which is null after first time. Try TrimExcess to get rid of null values: https://msdn.microsoft.com/en-us/library/ms132207(v=vs.110).aspx

      public GameObject GetPlatform() {
          print (plat.Count);
          if(plat.Count>0)
          {
              GameObject temp= plat[0];
              plat.RemoveAt(0);
              plat.TrimExcess();
              print ("GetPlatform called");
  //            temp.SetActive(true);
              return temp;
          }
          return null;
      }
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 DerperDoing · Mar 19, 2018 at 12:51 PM 0
Share

Hey thanks for the reply.
But the things is I did know why it was returning null.
$$anonymous$$y question was even after filling the list,say, with 5 elements in Start method when I checked the count of the list it is showing only one element is present in the list.
I've already printed the count in Start so I do know that it contains 5 elements for sure. But when I print count in GetPlatform method, it shows only 1 element.
I need help regarding this.

avatar image Lilius DerperDoing · Mar 19, 2018 at 02:52 PM 0
Share

Okay. This answer is completely wrong anyway :) Well your script works fine, just tested. $$anonymous$$aybe theres something wrong in the other class where you call the method GetPlatform? Since it prints count 5 when I tested it.

avatar image DerperDoing Lilius · Mar 23, 2018 at 04:30 PM 0
Share

Hey, sorry for the late reply. You mean the code I wrote is wrong?
GetPlatform is also printing 5 when you tested it?
This is how I'm calling it:

  public class Spawner : $$anonymous$$onoBehaviour {
         public GameObject op;
         ObjectPooler poolerScript2;
     
         float xPos,yPos;
         public float xRange=2;
         public float y$$anonymous$$in=0.01f;
         public float y$$anonymous$$ax= 1;
         
     
         void Start () {
             poolerScript2 = op.GetComponent<ObjectPooler> ();
         }
         
         // Update is called once per frame
         void Update () {
             xPos = Random.Range (-xRange, xRange);
             yPos += Random.Range (y$$anonymous$$in, y$$anonymous$$ax);
             GameObject platforms = poolerScript2.GetPlatform (poolerScript2.plat);
             platforms.transform.position = new Vector2 (xPos, yPos);
             platforms.SetActive (true);
         }
     }
     
 

   

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

129 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

How can I fix my object pool to continue after original pool is exhausted? 0 Answers

List give a count of zero when it have 1 element 1 Answer

Getting an Exception on Endless Runner 3D game 1 Answer

Code to replace the count commands in C# for text. 0 Answers

How to do this step without using ConvertAll ?? 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