- Home /
Deleting button when item removed from list?
Hi everyone, currently I am trying to change the list of buttons that show in a scrollview when certain conditions occur. I am doing this by creating two lists, and items created with an Object Pool switch between two lists. The items are created into ui buttons during runtime. Right now, the items transfer between the two lists perfectly fine, and the buttons are created fine. The issue is, my buttons aren't removing, and I don't know why. I have the function running in Start() and Update() so that it is running constantly. Also, in this code, I am trying to remove the button from the homePanel. So how do I remove the buttons that are equal to the specific item I am referring to? Here is my code:
public void hideUnmatchingDates(Item item)
{
for (int i = homePanel.itemList.Count - 1; i >= 0; i--) {
if (homePanel.itemList [i].dateButtonAdded.ToString () != logDate.text)
{
hiddenPanel.itemList.Add (homePanel.itemList [i]);
GameObject newButton = buttonObjectPool.GetObject ();
newButton.transform.SetParent (hiddenPanel.contentPanel);
ActivityBookButton activityButton = newButton.GetComponent<ActivityBookButton> ();
activityButton.Setup (homePanel.itemList [i], hiddenPanel);
homePanel.itemList.RemoveAt (i);
//button not removing with these lines for some reason
GameObject toRemove = transform.GetChild (i).gameObject;
buttonObjectPool.ReturnObject (toRemove);
Debug.Log ("item transfered to hidden panel");
}
}
}
Answer by KittenSnipes · May 07, 2018 at 04:24 AM
@jcfrizz So I know it is removing from the list and there is nothing wrong with your code. So I think the only solution is figuring out what you are trying to grab. So based on the call the object the script is attached to is where you are grabbing the child object for removal. So what is the parent object and what is the child? If you know what you want to remove then that will be your solution. If you need more help feel free to comment.
public void hideUnmatchingDates(Item item)
{
for (int i = homePanel.itemList.Count - 1; i >= 0; i--)
{
if (homePanel.itemList[i].dateButtonAdded.ToString() != logDate.text)
{
hiddenPanel.itemList.Add(homePanel.itemList[i]);
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(hiddenPanel.contentPanel);
ActivityBookButton activityButton = newButton.GetComponent<ActivityBookButton>();
activityButton.Setup(homePanel.itemList[i], hiddenPanel);
//Should remove it from list
homePanel.itemList.Remove(homePanel.itemList[i]);
//What transform is this attached to?
//button not removing with these lines for some reason
GameObject toRemove = transform.GetChild(i).gameObject;
buttonObjectPool.ReturnObject(toRemove);
Debug.Log("item transfered to hidden panel");
}
}
}
The parent is a game object that I call the content panel (in my code the two content panels are the "hiddenPanel" and "homePanel") which is what the script is attached to. The child is the button that is created in this content panel. In this area of the code, I am trying to remove a button (child) from the homePanel (parent). This button (child) is created with the information given by the item selected in the itemList. Does that make sense? It's sort of difficult to explain super well. Also, the content panel is the child of a scrollview viewport, so in the game, the buttons appear in a scrollable list for the user to be able to see.
But yeah I guess my issue is that I don't know how to get to in my code what it is that I'm trying to grab, and then how to delete it.
I actually figured it out!! Ins$$anonymous$$d of using the line
GameObject toRemove = transform.GetChild(i).gameObject;
I used ins$$anonymous$$d:
GameObject toRemove = homePanel.contentPanel.transform.GetChild(i).gameObject;
I think the issue was that I wasn't specifying what the transform was, which you pointed out. So once I said what the tranform was in my code, it worked!
Your answer
Follow this Question
Related Questions
What is the best way to handle a scenario game with a LOT of panels? 0 Answers
Looking for help with Scrolling List / rows for tv UI ( via game controller) 0 Answers
Assign images to list of buttons 0 Answers
Putting Dictionary/List using foreach as buttons in a scroll view? 3 Answers
UI Button : Previously working but now it's not working 0 Answers