# issues with AddUnique and arrays

**URL:** <https://forums.unrealengine.com/t/issues-with-addunique-and-arrays/471174>\
**Category:** Editor Scripting\
**Tags:** error, arrays, unreal-engine, question\
**Created:** [September 11, 2020, 10:49am UTC](https://forums.unrealengine.com/t/issues-with-addunique-and-arrays/471174 "2020-09-11T10:49:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 11, 2020, 10:49am UTC](https://forums.unrealengine.com/t/issues-with-addunique-and-arrays/471174/1 "2020-09-11T10:49:05Z")

</div>

This is giving me errors only when i add **CharacterHitResult.AddUnique(HitCharac)**

```
TArray<FHitResult> HitResult;

TArray<AShooterCharacter> CharacterHitResult;

for (const FHitResult& LoopHitResult : HitResult)
{

	AShooterCharacter* HitCharac = Cast<AShooterCharacter>(LoopHitResult.Actor);

	CharacterHitResult.AddUnique(*HitCharac);
}

```

` `

 ![313202-screenshot-2020-09-11-204449.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/6/f/c6f1a145e4fa17fb70c46d2bbf9f94b9758994ba.png)

---

<div class="post-metadata">

**Author:** ![Mahoukyou](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mahoukyou](https://forums.unrealengine.com/u/Mahoukyou)\
**Post date:** [September 11, 2020, 8:35pm UTC](https://forums.unrealengine.com/t/issues-with-addunique-and-arrays/471174/2 "2020-09-11T20:35:11Z")

</div>

TArray requires Type (AActor in this case) to be publicly copy constructible (public T(const T&) constructor), whereas AActors copy constructor is private. On top of that, AddUnique requires operator==(const T&) const to be defined, which AActor doesn’t have.

Apart from that, by dereferencing a pointer when adding it to an array, you would be creating a copy of the pointed object, thus the object in the array would not be the same that you added (well, it would be a copy of it, but it would no longer be the same object).

I would recommend changing the type of the TArray from AShooterCharacter to AShooterCharacter\* and storing a pointer to that object CharacterHitResult.AddUnique(HitCharac);

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_04b5ac74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_04b5ac74](https://forums.unrealengine.com/u/anonymous_user_04b5ac74)\
**Post date:** [September 12, 2020, 5:11am UTC](https://forums.unrealengine.com/t/issues-with-addunique-and-arrays/471174/3 "2020-09-12T05:11:22Z")

</div>

thanks that fixed that BUT it also caused some problems bellow

the error is cant convert ShooterCharacter\* to ShooterCharacter

```
for (const AShooterCharacter& ShooterCharRef : CharacterHitResult)
{
	FString PlayerName = ShooterCharRef.GetActorLabel();

	GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Yellow, PlayerName);
}

```

---

<div class="post-metadata">

**Author:** ![Mahoukyou](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mahoukyou](https://forums.unrealengine.com/u/Mahoukyou)\
**Post date:** [September 12, 2020, 7:19am UTC](https://forums.unrealengine.com/t/issues-with-addunique-and-arrays/471174/4 "2020-09-12T07:19:19Z")

</div>

In the code above you are trying to assign a pointer to a reference. You could do a reference to a pointer, but in this case it would be rather pointless. I recommend changing the type of ShooterCharRef to a pointer to AShooterCharacter. Before using it, it’s wise to check if the object is still valid and we are not dereferencing a tangling pointer (unless you are 100% sure it wasn’t deleted). Also note, that GetActorLabel function is only available in development builds, so I reckon that it won’t compile in cooked builds.

```
for (const AShooterCharacter* const ShooterChar : CharacterHitResult)
 {
     if(!IsValid(ShooterChar))
     {
          continue;
     }

     const FString& PlayerName = ShooterChar->GetActorLabel(); // or e.g. ShooterChar->GetName();
     GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Yellow, PlayerName);
 }
```
