# UE4 Having trouble Removing an actor from an array

**URL:** <https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405>\
**Category:** C++\
**Tags:** unreal-engine, question\
**Created:** [August 23, 2023, 11:44pm UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405 "2023-08-23T23:44:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug\_Scale](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@doug\_Scale](https://forums.unrealengine.com/u/doug_Scale)\
**Post date:** [August 23, 2023, 11:44pm UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405/1 "2023-08-23T23:44:56Z")

</div>

So I have an array of actor references that I want to remove an instance of. So I have these actors as a pickup item and I have it set up in code that when I run into it’s position the visual instance is removed but when I go back I still hear my pickup sound. Meaning that it still exists. I have tried to use remove method. The compiler says that remove() is not a part of this. Is there anything else I could use?

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [August 23, 2023, 11:47pm UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405/2 "2023-08-23T23:47:48Z")

</div>

Can you share the cpp?

---

<div class="post-metadata">

**Author:** ![doug\_Scale](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@doug\_Scale](https://forums.unrealengine.com/u/doug_Scale)\
**Post date:** [August 24, 2023, 12:10am UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405/3 "2023-08-24T00:10:09Z")

</div>

```auto
if (keysRef.Num() > 0) {
		for (int i = 0; i < keysRef.Num(); i++) {
			if (keysRef[i]->GetActorLocation() == FVector(newx, newy, 0)) {
				keysRef[i]->Destroy();
				
				cont.currentKey++;
				haskey = true;
				playPickup = true;
				break;
			}
		}
	}

```

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [August 24, 2023, 4:40am UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405/4 "2023-08-24T04:40:34Z")

</div>

> [@doug\_Scale](#):
>
> `if (keysRef.Num() > 0) {`

Unrelated to your issue: don’t think you need this conditions since loop will simply do nothing if the array is empty.

* * *

> [@doug\_Scale](#):
>
> `if (keysRef[i]->GetActorLocation() == FVector(newx, newy, 0))`

You won’t get consistent results (if any) by comparing doubles. Try using `FVector::PointsAreNear()`.

* * *

Try this if you don’t care about the order of items in the array:

```auto
	// Iterate TArray<TObjectPtr<AActor>> MyArray;
	for (auto Iterator = MyArray.CreateIterator(); Iterator; ++Iterator)
	{
		AActor& Element = **Iterator;
		// Check distance to destroy and remove from array.
		if (FVector::PointsAreNear(Element.GetActorLocation(), FVector(newx, newy, 0), 0.1))
		{
			// Destroy actor.
			Element.Destroy();
			// Swap to back and pop from array. --Iterator so next loop we get swapped element.
			Iterator.RemoveCurrentSwap();

			/* */
			
			break;
		}
	}

```

* * *

Let me know if it helps.

---

<div class="post-metadata">

**Author:** ![doug\_Scale](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@doug\_Scale](https://forums.unrealengine.com/u/doug_Scale)\
**Post date:** [August 24, 2023, 6:46pm UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405/5 "2023-08-24T18:46:12Z")

</div>

This works great but what if I am using GetAllActorsOfClass()?

---

<div class="post-metadata">

**Author:** ![doug\_Scale](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@doug\_Scale](https://forums.unrealengine.com/u/doug_Scale)\
**Post date:** [August 24, 2023, 7:22pm UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405/6 "2023-08-24T19:22:16Z")

</div>

I also have another problem I want to get rid of an actor reference, but I have an integer that is lined up with it

so I have these:

```auto
TArray<AActor*> doorsRef;
TArray<int> doornums;

```

I have an integer here because I need to know if the key opens the right door.

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [August 24, 2023, 7:27pm UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405/7 "2023-08-24T19:27:18Z")

</div>

Can you give more details on how it is supposed to work?

If I had to guess: player overlaps an actor, stores an ID that is later is used to trigger something on another actor?

---

<div class="post-metadata">

**Author:** ![doug\_Scale](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@doug\_Scale](https://forums.unrealengine.com/u/doug_Scale)\
**Post date:** [August 24, 2023, 7:33pm UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405/8 "2023-08-24T19:33:34Z")

</div>

What I mean is that when I delete the doorRef I need to get rid of the doornum that would be at the same index if it were doorRef[i].

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [August 24, 2023, 8:03pm UTC](https://forums.unrealengine.com/t/ue4-having-trouble-removing-an-actor-from-an-array/1270405/9 "2023-08-24T20:03:36Z")

</div>

> [@doug\_Scale](#):
>
> GetAllActorsOfClass()

`GetAllActorsOfClass()` returns an array of type `AActor*`, there is not difference as far as the loop is concerned.

> [@doug\_Scale](#):
>
> I need to get rid of the doornum that would be at the same index if it were doorRef[i].

With the example above you can get the current index with `Iterator.GetIndex()`. Should look something like this: `MyIntArray.RemoveAtSwap(Iterator.GetIndex());`
