# Why does {add item to index} go to the last slot?

**URL:** <https://forums.unrealengine.com/t/why-does-add-item-to-index-go-to-the-last-slot/482672>\
**Category:** Editor Scripting\
**Tags:** inventory, Structure, index, unreal-engine, Array, question, slot-name\
**Created:** [April 29, 2021, 8:51pm UTC](https://forums.unrealengine.com/t/why-does-add-item-to-index-go-to-the-last-slot/482672 "2021-04-29T20:51:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BluAstronaut](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BluAstronaut](https://forums.unrealengine.com/u/BluAstronaut)\
**Post date:** [April 29, 2021, 8:51pm UTC](https://forums.unrealengine.com/t/why-does-add-item-to-index-go-to-the-last-slot/482672/1 "2021-04-29T20:51:23Z")

</div>

When I pick up an item it’s supposed to add it to the next empty slot. However, it adds it to the last slot and goes to the top.

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [April 29, 2021, 9:02pm UTC](https://forums.unrealengine.com/t/why-does-add-item-to-index-go-to-the-last-slot/482672/2 "2021-04-29T21:02:54Z")

</div>

You’re removing the slot with ‘remove index’, and you’re using ‘add’, which puts items on the end anyway.

Try getting rid of both, and using SetArrayElem with that index integer.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [April 29, 2021, 9:03pm UTC](https://forums.unrealengine.com/t/why-does-add-item-to-index-go-to-the-last-slot/482672/3 "2021-04-29T21:03:18Z")

</div>

You find an empty slot, Remove it and then Add. If the empty slot happens to be in the middle of the array, that element gets removed, the array shrinks. And then you add at the end.

Did you mean to Insert the elements at index instead?

edit: I take it back. Set Array Elem makes most sense in this instance!

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [November 2, 2024, 3:40am UTC](https://forums.unrealengine.com/t/why-does-add-item-to-index-go-to-the-last-slot/482672/4 "2024-11-02T03:40:01Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
