# How to get the instance in the game in PlayerController?

**URL:** <https://forums.unrealengine.com/t/how-to-get-the-instance-in-the-game-in-playercontroller/2267803>\
**Category:** C++\
**Tags:** unreal-engine, question, CPP\
**Created:** [January 9, 2025, 1:45pm UTC](https://forums.unrealengine.com/t/how-to-get-the-instance-in-the-game-in-playercontroller/2267803 "2025-01-09T13:45:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenX](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevenX](https://forums.unrealengine.com/u/StevenX)\
**Post date:** [January 9, 2025, 1:45pm UTC](https://forums.unrealengine.com/t/how-to-get-the-instance-in-the-game-in-playercontroller/2267803/1 "2025-01-09T13:45:22Z")

</div>

I am working on the logic for an elevator, and I want the player to press the ‘E’ key after entering a specific area, which will make the elevator move up or down. To achieve this, I want to call the elevator class logic from the PlayerController. However, it seems that the PlayerController is created before the elevator, and I cannot access the elevator instance in the game. Is there a way to solve this?

---

<div class="post-metadata">

**Author:** ![SpecZynk](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/speczynk/32/1117840_2.png) [@SpecZynk](https://forums.unrealengine.com/u/SpecZynk)\
**Post date:** [January 13, 2025, 6:14pm UTC](https://forums.unrealengine.com/t/how-to-get-the-instance-in-the-game-in-playercontroller/2267803/2 "2025-01-13T18:14:24Z")

</div>

Hi

In order to get your `PlayerController` you can use the Pawn Template function `GetController` or simple use `UGameplayStatics`.

In your pawn derived class:

```auto
APlayerController* PC = GetController<APlayerController>();

```

Using your pawn’s reference:

```auto
APlayerController* PC = MyPawnRef->GetController<APlayerController>();

```

Using UGameplayStatics:

```auto
APlayerController* PC = UGameplayStatics::GetPlayerController(this, 0);

```

bye.

---

<div class="post-metadata">

**Author:** ![StevenX](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevenX](https://forums.unrealengine.com/u/StevenX)\
**Post date:** [January 16, 2025, 5:03am UTC](https://forums.unrealengine.com/t/how-to-get-the-instance-in-the-game-in-playercontroller/2267803/3 "2025-01-16T05:03:25Z")

</div>

No, I mean that the player controller cannot access the instances of other classes during the game, and it cannot get the elevator instance at the start of the game.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [January 17, 2025, 4:11pm UTC](https://forums.unrealengine.com/t/how-to-get-the-instance-in-the-game-in-playercontroller/2267803/4 "2025-01-17T16:11:30Z")

</div>

Line trace from the player controller towards the center of the screen and get a hit result actor.  
Then call an interface on the hit actor.  
Have the elevator implement the interface and have functions to react to the interaction.

No need to keep a reference of the elevator inside of the controller.

In this case the interaction with the elevator can show a ui with floors to pick.  
Or you can have a 3d widget in worldspace that can have buttons to change floors.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [February 16, 2025, 4:12pm UTC](https://forums.unrealengine.com/t/how-to-get-the-instance-in-the-game-in-playercontroller/2267803/5 "2025-02-16T16:12:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
