# Cast object to class variable in Blueprint?

**URL:** <https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392>\
**Category:** Blueprint\
**Tags:** unreal-engine, question\
**Created:** [April 30, 2024, 4:57pm UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392 "2024-04-30T16:57:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krabworks](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/krabworks/32/1194549_2.png) [@Krabworks](https://forums.unrealengine.com/u/Krabworks)\
**Post date:** [April 30, 2024, 4:57pm UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/1 "2024-04-30T16:57:49Z")

</div>

I am wondering if there is a way to cast an object reference to a class by a class variable reference.

This is possible in Blueprint it seems for classes using the following:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/3/f/43fea78a67e6b3219fa6bb25d9d091f27ef084aa.png)

However, there does not seem to be an equivalent node for some reason to return an _ **object reference** _ (only a class reference). I can use a regular cast node to do this most of the time, but this requires that I know in advance the type of class I want to cast to, and it therefore can’t be set later since there is no option to do the same as above and return an object reference of that class variable type.

Any way around this limitation? Seems like an oversight to not have any node for such an obvious use-case. I could use a BPI, but this requires a lot of maintenance since it needs to be added to every class that would want to use this functionality…and anyways, if “Cast to Class” already exists, surely there is a node for this returning an object reference of that class and I just can’t find it?

EDIT: To clarify - I am aware that you cannot objects dynamically. This is mostly for convenience of being able to set a character class through class defaults. But perhaps because people would try to set it at runtime is why it may not exist?

---

<div class="post-metadata">

**Author:** ![Auran131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Auran131](https://forums.unrealengine.com/u/Auran131)\
**Post date:** [May 1, 2024, 3:28am UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/2 "2024-05-01T03:28:58Z")

</div>

Do you mean you want GetClass() ?

---

<div class="post-metadata">

**Author:** ![Krabworks](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/krabworks/32/1194549_2.png) [@Krabworks](https://forums.unrealengine.com/u/Krabworks)\
**Post date:** [May 1, 2024, 4:19am UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/3 "2024-05-01T04:19:47Z")

</div>

No; I want to get an object reference of that class. I.e., Cast to class, but with a class variable input. Right now this is not possible it seems?

There are separate cast nodes for each class - but not a single cast node that lets you choose a class to cast to. Hopefully that makes sense.

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/a/5/0a554a886e8a9a014d554667216ed06580657bcb.png)

There is a variant (in my original post above) that lets you do this and returns a class reference, but not one that lets you do this and returns an object reference.

---

<div class="post-metadata">

**Author:** ![DomusLudus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/domusludus/32/1215140_2.png) [@DomusLudus](https://forums.unrealengine.com/u/DomusLudus)\
**Post date:** [May 1, 2024, 4:30am UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/4 "2024-05-01T04:30:39Z")

</div>

but, what do you want to do? this looks like an XY problem

maybe If you don’t know the class to cast, you could use an interface

---

<div class="post-metadata">

**Author:** ![Krabworks](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/krabworks/32/1194549_2.png) [@Krabworks](https://forums.unrealengine.com/u/Krabworks)\
**Post date:** [May 1, 2024, 4:47am UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/5 "2024-05-01T04:47:31Z")

</div>

I did say right above that an interface is not the most convenient :'D This has already been addressed.

To clarify - what I want to do is _avoid_ setting this up using interfaces. This is for a component class that could be added to any character.

Now, part of the point of a composition is that you design something that can be added to any actor class. This would achieve the goal of composition; a BPI will not. It would need to be added on a case-by-case basis, and the above node (if some alternative exists) would avoid that blueprint limitation.

---

<div class="post-metadata">

**Author:** ![Auran131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Auran131](https://forums.unrealengine.com/u/Auran131)\
**Post date:** [May 1, 2024, 6:08am UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/6 "2024-05-01T06:08:01Z")

</div>

You can Use GetComponentByClass() then?

---

<div class="post-metadata">

**Author:** ![Krabworks](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/krabworks/32/1194549_2.png) [@Krabworks](https://forums.unrealengine.com/u/Krabworks)\
**Post date:** [May 1, 2024, 7:43pm UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/8 "2024-05-01T19:43:14Z")

</div>

That’s the idea I’m going for, yes - so that the component can be retrieved this way later without having to also add BPIs etc. to the actor that should use it.

However that still doesn’t return a reference to an actor that the component goes on. That would return a component only, not a reference to any other class - for example a character or another actor whose class you might want to specify by variable that would be required to use the component.

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [May 2, 2024, 9:32am UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/9 "2024-05-02T09:32:45Z")

</div>

> [@Krabworks](#):
>
> There are separate cast nodes for each class - but not a single cast node that lets you choose a class to cast to. Hopefully that makes sense.

That doesn’t make sense unfortunately.  
The point of casting is to access specific properties and functions that only exist in the casted class type.  
If your class is dynamic, which properties/functions are you gonna use ?  
If your goal is just to test inheritance, use ClassIsChildOf

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/d/2/0d2a8336eda707f2f542d310a8df66d6208f2d5f.png)

* * *

> [@Krabworks](#):
>
> However, there does not seem to be an equivalent node for some reason to return an _ **object reference** _ (only a class reference). I can use a regular cast node to do this most of the time

Cast doesn’t magically give you an object reference out of nowhere.  
It transforms the object reference you give as input, after checking that it is of the right class.  
Cast is merely a way of checking validity of a given object, before accessing properties/functions that are specific to a certain subclass.

* * *

> [@Krabworks](#):
>
> I want to get an object reference of that class

There must be an existing object of that class in the first place, instanced in the world.  
If there is, you can use GetActorOfClass (for actors) or GetAllWidgetsOfClass (for widgets) or GetComponentByClass to attempt to find it.  
All of these accept a variable class input :

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/d/6/fd6825645fe5e63dcb4f3209837abbc36ef8c5f6.png)

* * *

> [@Krabworks](#):
>
> However that still doesn’t return a reference to an actor that the component goes on. That would return a component only

If you have a component, you also have its owning actor.  
GetComponentOfClass requires an actor input, so technically you already have the actor reference when you call that.  
But even if you don’t, you can still use GetOwner on the component to go back to the actor :

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/1/f/a1f2d863269a30e6feec1db9d8f59fc4189cf963.png)

> [@Krabworks](#):
>
> for example a character or another actor whose class you might want to specify by variable that would be required to use the component.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/f/a/afadd8c22c8adce769ed6f73c540ec4a0892f242.png)

---

<div class="post-metadata">

**Author:** ![Engelard](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/engelard/32/903560_2.png) [@Engelard](https://forums.unrealengine.com/u/Engelard)\
**Post date:** [May 15, 2025, 8:35am UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/10 "2025-05-15T08:35:00Z")

</div>

The point of this thread was that he wants stuff you made on your screenshot, but not using your 6 nodes but just 1 - `CastObjectToClass`.

Here is what OP meant:

 ![UnrealEditor_4XuDeGQhLD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/9/8/e987823a8559a69a161d7253dd77dd1b1c8c7d68.png)

If such a convenience would exist(it should, it is intuitively expected), then in UE Blueprints wouldn’t be **HUNDREDS** of these:

 ![UnrealEditor_3zNeZlc5t9](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/8/3/783a97514243284f2aac6a0ed209dc94a3195f6f.png)

Instead, there would be **just a single one** function, with a Class for the Cast specified as a parameter. It not just would be more convenient, it would also allow specify Class at runtime, which we are not able now unfortunately.

---

<div class="post-metadata">

**Author:** ![SCARBLAZ1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SCARBLAZ1](https://forums.unrealengine.com/u/SCARBLAZ1)\
**Post date:** [August 13, 2025, 3:43pm UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/11 "2025-08-13T15:43:12Z")

</div>

I know its long time, but you ever succeded what you were trying to do?  
am trying to do exactly what you explained in that image

---

<div class="post-metadata">

**Author:** ![Auran131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Auran131](https://forums.unrealengine.com/u/Auran131)\
**Post date:** [August 13, 2025, 4:19pm UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/12 "2025-08-13T16:19:32Z")

</div>

yes you can in c++ using DeterminesOutputType

---

<div class="post-metadata">

**Author:** ![Krabworks](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/krabworks/32/1194549_2.png) [@Krabworks](https://forums.unrealengine.com/u/Krabworks)\
**Post date:** [August 13, 2025, 4:21pm UTC](https://forums.unrealengine.com/t/cast-object-to-class-variable-in-blueprint/1833392/13 "2025-08-13T16:21:34Z")

</div>

^^ This is the answer I believe. There is no Blueprint node AFAIK, you can do it in C++ or re-think your function to avoid this.
