How to access selected object in list (Example 14)

0 votes
asked Oct 16, 2016 in Help by Ed Miller

I'm using the Simple List Big Sprite picker (example 14) to select a GameObject from Resources with the same filename as the sprite (minus "_icon" etc).

However, I'm finding it hard to see how to reference the selected object. So far I've got the following - 

            activeObjectString = m_IconSpriteArray[selectedIndex].name;

            // Set prefab, using activeObjectString.
            activePrefab = Resources.Load("Prefabs/" + activeObjectString, typeof(GameObject));

Can you suggest what I'm doing wrong? What is the best way to access the selected object from a list?

commented Oct 22, 2016 by admin (31,720 points)
Hi Ed,

How are you creating/showing the dialog?

~ Declan.
commented Oct 22, 2016 by anonymous
Thanks for replying Declan

I'm currently showing the dialogue with a large sprite list, taken from Example 14, which pops up a toast dialogue box. I'm using this list to select which prefab to make 'active' for a GameObject. The Prefab has the same name as the icon, minus the string "icon_".

FYI, eventually, I'll want to replace this with a custom dialogue that pops up from the bottom, taking up 1/3 of the screen vertically, showing a single row of sprites, which is scrollable horizontally.

Thanks for your continued support.
commented Oct 24, 2016 by admin (31,720 points)
Hi,

Does the name of the sprite need to match the prefab? Could you instead have an array of references to the prefabs, and use the selected index from the dialog to select the prefab by index?

~ Declan.
commented Oct 24, 2016 by anonymous
That could work.

I thought keeping the name of the sprite to match the prefab would be more intuitive as it would be easier to pair sprite name with prefab name, rather than array number with prefab name. However, I'm just keen to get something working for the time being so don't really mind!

Would I create a public array and drag and drop the prefabs in via the editor? Do I need to write any additional code?
commented Oct 24, 2016 by admin (31,720 points)

Hi,

Sorry, I now see the benefit of using the name to access the prefab you want.

What you have should be almost right - assuming the name of the sprite is the same as the prefab name + "_icon", then you can change that last line to be:

activePrefab = Resources.Load("Prefabs/" + activeObjectString.Replace("_icon", ""), typeof(GameObject));

and it should work :)

~ Declan.

1 Answer

0 votes
answered Nov 24, 2016 by admin (31,720 points)

Hi,

What you have should be almost right - assuming the name of the sprite is the same as the prefab name + "_icon", then you can change that last line to be:

activePrefab = Resources.Load("Prefabs/" + activeObjectString.Replace("_icon", ""), typeof(GameObject));

and it should work :)

~ Declan.

Welcome to MaterialUI support! Ask us anything :)
...