Struggling with GetComponentsByTag

OtherActor->GetComponentsByTag(TSubclassOf, )

I have been struggling with what to put and i would like to use the box component.
Thank you

Here is an example, that will get an array of box components components with the “tag1” tag and then log their names:

TArray<UActorComponent*> Comps = GetComponentsByTag(UBoxComponent::StaticClass(), "tag1");

for (const auto& component : Comps)
{
	UE_LOG(LogTemp, Warning, TEXT("Comps > %s"), *component->GetName());
}
1 Like

if(OtherActor->GetComponentsByTag(GetComponentsByTag(UBoxComponent::StaticClass(), “tag1”))
{

        }

I have tried other ways and i cant figure it out, not sure why

Like I said before “GetComponentsByTag, returns a TArray, so why are you checking that in an if?

If you want to check an array, check its size:

if (OtherActor->GetComponentsByTag(UBoxComponent::StaticClass(), "tag1").Num() > 0)
{
    // there are Box components with that tag
}

Yes thank you. that was me being dumb

What is the problem and why do you have GetComponentsByTag twice?

Also, GetComponentsByTag, returns a TArray, so why are you checking that in an if? What are you trying to achieve?

sorry i didnt mean to put it twice but the error is = error: use of undeclared identifier ‘UBoxComponent’

Did you included the BoxComponent header?

#include "Components/BoxComponent.h"

Now i am getting this error: error: value of type ‘TArray’ is not contextually convertible to ‘bool’

You probably check the for the presence of the TArray, not its content.
Try something like

TArray.IsEmpty()