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 lukenerdhole · Dec 02, 2018 at 08:47 PM · errorunity 2dinventory systemwhile-loop

Stuck in a while loop inside my inventory

I'm probably being silly and not seeing the obvious but.

I'm using the Simple Object Pool described in https://unity3d.com/learn/tutorials/topics/user-interface-ui/shopscrolllist-script plus JSON files to create an inventory of buttons.

I'm trying to use the RemoveButtons() to clear the list and repopulate it with another list when another button is selected.

public class InventoryController : MonoBehaviour {

 public List<Item> itemListAll = new List<Item>();
 //public List<Item> itemListMisc = new List<Item>();
 public Transform contentPanel;
 public InventoryController otherShop;
 public Text myGoldDisplay;
 public SimpleObjectPool buttonObjectPool;
 public Text myDescriptionDisplay;
 public Text myItemName;
 public Image myitemimage;
 public Text myitemeffect;

 //public List<Item> CurrentList = new List<Item>();

 public float gold = 20f;

 public int buttonNumber;

   
 void Awake()
 {
     
         ConstructFullItemDatabase();
     

 }

 void RefreshDisplay()
 {
     
     myGoldDisplay.text = "All Items";     
     RemoveButtons();
     AddButtons();
    
 }
 public void ConstructFullItemDatabase()
 {
     
     
     itemListAll = JsonConvert.DeserializeObject<List<Item>>(Resources.Load<TextAsset>("Streaming Assets/PlayerInventoryAll").ToString());
     RefreshDisplay();
     

 }

 public void RemoveButtons()
 {
     
     while (contentPanel.childCount > 0)
     {
         
         GameObject toRemove = contentPanel.GetChild(0).gameObject;
         buttonObjectPool.ReturnObject(toRemove);
         
     }
 }



 private void AddButtons()
 {
     for (int i = 0; i < itemListAll.Count; i++)
     {
         Item item = itemListAll[i];
         GameObject newButton = buttonObjectPool.GetObject();
         newButton.transform.SetParent(contentPanel, false);

         SampleButton sampleButton = newButton.GetComponent<SampleButton>();
         sampleButton.Setup(item, this);
     }
 }


inside the simpleobjectpool.cs

 public void ReturnObject(GameObject toReturn)
 {
     
     PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
     
     // if the instance came from this pool, return it to the pool
     if (pooledObject != null && pooledObject.pool == this)
     {
        
         // disable the instance
         toReturn.SetActive(false);
         
         // add the instance to the collection of inactive instances
         inactiveInstances.Push(toReturn);
         
     }
     // otherwise, just destroy it
     else
     {
         Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
         Destroy(toReturn);
     }
 }

It seems to be stuck on the while loop and never returning the samplebuttons back to the object pool. I've tried calling the ReturnObject seperately and its working when I choose the object to return. It should loop through all the children in the contentPanel and keep returning the one at position 0. But instead it's getting stuck.

Any help would be fantastic.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by lukenerdhole · Dec 04, 2018 at 03:30 PM

I don't know if its much of a solution but I removed the while statement and put a for loop in instead.

 public void RemoveButtons()
     
 {
     for(int j = 0; j < contentPanel.childCount; j++)
     {
         GameObject toRemove = contentPanel.GetChild(j).gameObject;
         //buttonObjectPool.ReturnObject(toRemove);
         Destroy(toRemove);
     }
     
 }

and changed the SimpleObjectPool to

 public void ReturnObject(GameObject toReturn)
 {
   toReturn.SetActive(false);
   inactiveInstances.Push(toReturn);
  }

instead of all the other stuff that came with it about specific pools... not sure about any negative effects this might have but it seems to be working atm. I can deactivate the buttons and reuse them for the other items in my characters json file inventory lists.

Comment
Add comment · 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
0

Answer by waizui · Mar 11, 2019 at 12:28 PM

stuck in same place ,but destroy is too expensive right? why can't just simply push objects to pool without destroy it?

Comment
Add comment · 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

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

188 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 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

Change trail rederer to it's default/original vaule. 0 Answers

assembly csharp error 0 Answers

Unity error DirectoryNotFoundException: Could not find a part of the path : 0 Answers

Android Game Save and Load doesn't work 0 Answers

Compiler error 0 Answers


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