0

I was wondering if someone could explain to me how I update a list at runtime in Unity?

For example I have a spell book, which has 3 spells in it, when I drag them to my action bar they get re-parented to it, what I'm trying to do is get a list of each child under the actionbar transform,

My problem is where to actually do something like this if I try something like below in the update, it adds whatever's there many times, which I can understand since update runs every frame..

list = actionbarui.GetComponent(UIGrid).GetChildList();

for(var i =0;i < list.size; i++)
    {
    buttonList.Add(list[i].gameObject);
    }

If I add a callback to the buttons so that whenever they're drag and dropped it runs the above code and add thing's additively, as in, if you drag just one "spell" on it will loop through once, which is fine, but as soon as you add another it loops through three times, that is, it already has one it then adds the same one again PLUS the new button that's just been dragged on for a total of 3.

I've tried changing the code to

list = actionbarui.GetComponent(UIGrid).GetChildList();

for each(var child : Transform in list.GetEnumerator())
    {
    buttonList.Add(child.gameObject);
    }

But this simple doesn't add anything, I am unsure how to go about keep the lists update as they are dragged off and on, could anyone please explain how this is achieved?

Please ignore the second "list" code, it's implementing "BetterLists" as apart of NGUI, so doesn't follow standard list methods.

Thank you for reading

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Pheonix2105
  • 1,001
  • 2
  • 9
  • 24
  • Please stop editing the question, making stupid minor changes so you can get some silly badge. – Pheonix2105 May 06 '14 at 15:51
  • 2
    It's not stupid change, you're misleading Unity as a injection container for .NET with Unity3d as a game engine. If you want to have correct answers and viewers, you should make sure that you've used correct tag. – kreys May 07 '14 at 07:23

2 Answers2

0

If I add a callback to the buttons so that whenever they're drag and dropped it runs the above code and add thing's additively, as in, if you drag just one "spell" on it will loop through once, which is fine, but as soon as you add another it loops through three times, that is, it already has one it then adds the same one again PLUS the new button that's just been dragged on for a total of 3.

That is the expected result here. You are running through the list of children and adding them to your buttonlist, so first time there is only one item to add, then when you add another child there are two items to add, thus resulting in three items.

Either don't have a for loop and do:

buttonList.Add(the_GameObject_that_was_just_added_to_the_bar);

Or clear your buttonList before the for loop:

list = actionbarui.GetComponent(UIGrid).GetChildList();
buttonList.Clear();
for(var i =0;i < list.size; i++)
{
    buttonList.Add(list[i].gameObject);
}
Dover8
  • 607
  • 3
  • 17
0

Alternatively to Dover8's response, you can first check if the buttonList already contains a reference to the child to be added. If it does, continue. Otherwise add the new child.

However, given the expense of doing the checks, and since you are already looping through the list already, I'd recommend that you follow his suggestion to clear the list before running the loop. It will probably give you the best performance out of these examples.

If there's a reason you can't or shouldn't clear the buttonList, and you still need to run through it, I'd recommend my suggestion. It really depends on which implementation works best for what you are trying to do. The point is that there are several ways to do this, and they have different performance profiles you need to consider.

memBrain
  • 33
  • 7