# Emty object array returns length 1

**URL:** <https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [August 26, 2018, 4:43pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499 "2018-08-26T16:43:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [August 26, 2018, 4:43pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/1 "2018-08-26T16:43:48Z")

</div>

Hey all,

when i try to get the length of an (empty) object array it always returns 1. What is up with that? Is this behavior intended?

Best regards,

Maffinius

---

<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:** [August 26, 2018, 6:10pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/2 "2018-08-26T18:10:33Z")

</div>

No, not intended. Create a new variable, make it into an array, get length, you’ll get 0.

Show us how you handle the array you find problematic.

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [August 26, 2018, 6:48pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/3 "2018-08-26T18:48:58Z")

</div>

![251637-ue4screens.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/1/3/413fe7ebe8177afb82cc0a1db7cfd63104ed4315.png)

 ![251638-ue4screens2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/6/8/968acd8bfd4956e4589bb67d6107178cdee4775c.png)

The array is not used so far and empty. String-output: Arraylength: 1

Thanks in advance 🙂

---

<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:** [August 26, 2018, 6:57pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/4 "2018-08-26T18:57:05Z")

</div>

Looks good. This would only print 1 if you dynamically spawned and added a worker. You sure nothing like that happens somewhere on a forgotten Begin Play? A testing leftover of sorts?

As an easy debug, open a _New Task Villager_ blueprint and Prrint String something on its Begin Play event. Does it spawn?

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [August 26, 2018, 7:19pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/5 "2018-08-26T19:19:33Z")

</div>

![251640-ue4screens3.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/0/2/e021115f45dd3fb3ae23fa5a4b593f18e979854b.png)

![251651-ue4screens5.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/c/f/4cfe8f462cfeb1967e63e95abb21404c71dcdb1f.png)

I also print the arrays content right after printing out the length. Nothing is printed out and the length is still 1 ☹  
I also double checked all begin play’s and event graph’s (even though i’m 100% sure that i nowhere create or add villagers :/)

I also print a string in the begin play like u suggested but no villager is spawned somewhere

---

<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:** [August 26, 2018, 7:26pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/6 "2018-08-26T19:26:50Z")

</div>

Weird, as test, can you call _Clear_ on the array and then print it?

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [August 26, 2018, 7:45pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/7 "2018-08-26T19:45:37Z")

</div>

Ok when i call clear directly before calling the printArray function its has no impact.  
If i call clear in the printArray function (directly before getting the length) i get length = 0. My suggestion was that i maybe do something wrong in eventTick but there is nothing that has anything to do with the villagers.

EDIT: i changed the arraytype from Unit\_BP array to Object array and now i can call clear the array before calling the function to get length = 0. but the main bug is still happening

---

<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:** [August 26, 2018, 10:42pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/8 "2018-08-26T22:42:12Z")

</div>

One more thing you could try is to Print it in Tick and observe how early it becomes 1 in the log.

Also, do you know why you have _World Context Object_ on your nodes? Haven’t seen this in years…

This is not a pure blueprint project, right?

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [August 27, 2018, 10:15am UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/9 "2018-08-27T10:15:10Z")

</div>

OK i looked up my right mouse event again since the length was set to 1 every time i pressed it. It appears that i made a new array (“make array”) with no content at the end. I use “clear” now and it works perfectly fine.

Thanks for youre patencience and help 😃

PS: Is it intended to have length 1 when i make a new array with no content?

---

<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:** [August 27, 2018, 10:16am UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/10 "2018-08-27T10:16:36Z")

</div>

No, definitely no. Can you show a screenshot of how you do it? _Make Array_ has a very specific use, perhaps it is not being used right.

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [August 27, 2018, 10:51am UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/11 "2018-08-27T10:51:15Z")

</div>

This happens sometimes if you drag an actor variable and select the print string node directly, without ToString() function. it creates this input pin. And it may sometimes stay there even if disconnected.

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [August 27, 2018, 12:51pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/12 "2018-08-27T12:51:34Z")

</div>

![251702-ue4screens6.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/0/9/b09f24b2191645b00ad35118e8323a775ac5c925.png)

I used “make array” like this.  
EDIT: Just saw the “remove array element pin” option … this works aswell ^^

---

<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:** [August 27, 2018, 1:15pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/13 "2018-08-27T13:15:38Z")

</div>

There we go. Your array now has 1 element at index zero. You do not need this at all and it’s actually quite dangerous to use it like this for actors as you create a null pointer.

By simply having this variable, you already have an empty array - no need to make array. Try it.

---

<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:** [August 27, 2018, 3:41pm UTC](https://forums.unrealengine.com/t/emty-object-array-returns-length-1/432499/14 "2018-08-27T15:41:40Z")

</div>

@ Tuerer: Thanks! It seems like it does not do much anyway.
