# Can't access function in another class C++

**URL:** <https://forums.unrealengine.com/t/cant-access-function-in-another-class-c/483169>\
**Category:** C++\
**Tags:** casting, unreal-engine, pointer, functions, question, CPP\
**Created:** [May 10, 2021, 11:03am UTC](https://forums.unrealengine.com/t/cant-access-function-in-another-class-c/483169 "2021-05-10T11:03:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![h0pperh0pper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@h0pperh0pper](https://forums.unrealengine.com/u/h0pperh0pper)\
**Post date:** [May 10, 2021, 11:03am UTC](https://forums.unrealengine.com/t/cant-access-function-in-another-class-c/483169/1 "2021-05-10T11:03:32Z")

</div>

Hi guys, I am just trying a call the function Death() which is located in Sentinal.cpp from Shrouded.cpp using a cast. Sentinal.h has been included within the Shrouded.cpp file. However when it is called with Sentinal-\>Death(); it returns null.

Any help would be appreciated, I am able to provide more if needed.

**Shrouded.cpp**

```
void AShrouded::Melee()
{
	GetWorldTimerManager().SetTimer(ShroudHandle, this, &AShrouded::Unshroud, 0.1, false);

	FVector Location;
	FRotator Rotation;
	FHitResult Hit;
	FString HitActor;

	GetController()->GetPlayerViewPoint(Location, Rotation);

	FVector Start = Location;
	FVector End = Start + (Rotation.Vector() * MeleeDistance);

	FCollisionQueryParams TraceParams;
	bool bHit = GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility, TraceParams);

	if (bHit)
	{
		DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 2.0f);
		DrawDebugBox(GetWorld(), Hit.ImpactPoint, FVector(2, 2, 2), FColor::Red, false, 2.0f);

		ASentinal* Sentinal = Cast<ASentinal>(GetOwner());
		if (Sentinal != nullptr)
		{
			Sentinal->Death();
		}
		else {
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("null"));
		}
		

	}
}

```

**Sentinal.h**

```
	UFUNCTION()
		void Death();

```

**Sentinal.cpp**

```
void ASentinal::Death()
{
	
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("You killed a Sentinal"));
	GetMesh()->SetAllBodiesSimulatePhysics(true);
	GetMesh()->SetSimulatePhysics(true);
	GetMesh()->WakeAllRigidBodies();
	GetMesh()->bBlendPhysics = true;
	
}
```

---

<div class="post-metadata">

**Author:** ![Fraps2020](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fraps2020/32/460865_2.png) [@Fraps2020](https://forums.unrealengine.com/u/Fraps2020)\
**Post date:** [May 10, 2021, 11:32am UTC](https://forums.unrealengine.com/t/cant-access-function-in-another-class-c/483169/2 "2021-05-10T11:32:03Z")

</div>

Chances are that Sentinal is _not_ owning Shrouded, reason why your GetOwner() doesn’t return what you expect.  
If I understand correctly a Sentinal is what you’re trying to hit; try to cast Hit.Actor instead.  
Hope that helps.

---

<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:** [May 10, 2021, 11:34am UTC](https://forums.unrealengine.com/t/cant-access-function-in-another-class-c/483169/3 "2021-05-10T11:34:17Z")

</div>

Is Shrouded a component of Sentinal? I assume GetOwner() doesn’t return what you expect it to return, and that’s why casting fails.

---

<div class="post-metadata">

**Author:** ![h0pperh0pper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@h0pperh0pper](https://forums.unrealengine.com/u/h0pperh0pper)\
**Post date:** [May 10, 2021, 12:15pm UTC](https://forums.unrealengine.com/t/cant-access-function-in-another-class-c/483169/4 "2021-05-10T12:15:45Z")

</div>

No they’re two seperate classes. Yeah I’m not sure if GetOwner was the play Death() is just a simple function in Sentinal.

---

<div class="post-metadata">

**Author:** ![h0pperh0pper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@h0pperh0pper](https://forums.unrealengine.com/u/h0pperh0pper)\
**Post date:** [May 10, 2021, 12:16pm UTC](https://forums.unrealengine.com/t/cant-access-function-in-another-class-c/483169/5 "2021-05-10T12:16:52Z")

</div>

I’m not sure because im not using the standard on hit but rather a line trace to determine whether or not it has hit another character.

---

<div class="post-metadata">

**Author:** ![h0pperh0pper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@h0pperh0pper](https://forums.unrealengine.com/u/h0pperh0pper)\
**Post date:** [May 10, 2021, 12:34pm UTC](https://forums.unrealengine.com/t/cant-access-function-in-another-class-c/483169/6 "2021-05-10T12:34:53Z")

</div>

You sent me on the right track fraps! I had to case Hit.GetActor() instead.  
Here’s the code:

```
	if (bHit)
	{
		DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 2.0f);
		DrawDebugBox(GetWorld(), Hit.ImpactPoint, FVector(2, 2, 2), FColor::Red, false, 2.0f);

		ASentinal* Sentinal = Cast<ASentinal>(Hit.GetActor());
		if (Sentinal != nullptr)
		{
			Sentinal->Death();
		}
		else {
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("null"));
		}
		

	}
```
