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.
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.
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?
Your answer
Follow this Question
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