# Creating a simple object in UE4 C++ code does not work

**URL:** <https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [September 11, 2016, 10:12am UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083 "2016-09-11T10:12:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jibera](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jibera](https://forums.unrealengine.com/u/Jibera)\
**Post date:** [September 11, 2016, 10:12am UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083/1 "2016-09-11T10:12:05Z")

</div>

Hey guys, I cant seem to make create a simple object in ue4 c++.  
I have these two different classes:  
RandomGenActor.h - \> UClass of AActor  
RandomGenActor.cpp

and another  
CarClass.h → is just a blank class  
CarClass.cpp

And what I want to do is to create an object of CarClass inside RandomGenActor, here’s what I want to do:  
to declare

```
CarClass * CarC = new CarClass;

```

inside RoomGenActor

but I’m getting compilation errors, any idea what is the proper way to do this? Thanks!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_aa2a7fa1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_aa2a7fa1](https://forums.unrealengine.com/u/anonymous_user_aa2a7fa1)\
**Post date:** [September 11, 2016, 8:20pm UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083/2 "2016-09-11T20:20:06Z")

</div>

While that’s the typical C++ way of creating a new object, you want to use UWorld::SpawnActor so that it registers your object in the engine properly.

Here’s a reference: [Spawning Actors in Unreal Engine | Unreal Engine 5.1 Documentation](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/Spawning/)

---

<div class="post-metadata">

**Author:** ![Jibera](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jibera](https://forums.unrealengine.com/u/Jibera)\
**Post date:** [September 12, 2016, 2:48am UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083/3 "2016-09-12T02:48:10Z")

</div>

Does it really have to be an actor? Is there no other way?  
I’ve come across this post but the method that I tried here doesnt work, I havent tried constructing the object like the second method mentioned here yet.

> **[How to create a simple object in C++ with UE4?](https://forums.unrealengine.com/questions/110369/how-to-create-a-simple-object-in-c-with-ue4.html)**
>
> Hi. I'm trying to create a simple object in C++. It's not an entity from the world, just a class with variables and functions which I have in the the player character. But I needed to make it as a Pawn so I can access it from the blueprints. This...

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d24c3c621](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d24c3c621](https://forums.unrealengine.com/u/anonymous_user_d24c3c621)\
**Post date:** [September 12, 2016, 12:23pm UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083/4 "2016-09-12T12:23:39Z")

</div>

Make your class inheriting UObject and use the NewObject() method to create your derived UObject.

> **[UObject Instance Creation](https://docs.unrealengine.com/5.2/en-US/creating-objects-in-unreal-engine/)**
>
> Methods of creating new instances of Objects in gameplay code.

---

<div class="post-metadata">

**Author:** ![Jibera](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jibera](https://forums.unrealengine.com/u/Jibera)\
**Post date:** [September 12, 2016, 2:19pm UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083/5 "2016-09-12T14:19:24Z")

</div>

oh okay, I’ll try that.  
Anyways, is it also okay if I just code all objects in different classes and just have them communicate in a blueprint if thats possible?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_aa2a7fa1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_aa2a7fa1](https://forums.unrealengine.com/u/anonymous_user_aa2a7fa1)\
**Post date:** [September 13, 2016, 6:18pm UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083/6 "2016-09-13T18:18:48Z")

</div>

Yeah that’s possible, you can create blueprint\_implementable functions in your class and expose all the member variables to blueprints, but it might be more work than it’s worth, depending on what you’re trying to accomplish.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9a89b67](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9a89b67](https://forums.unrealengine.com/u/anonymous_user_b9a89b67)\
**Post date:** [September 14, 2016, 7:50am UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083/7 "2016-09-14T07:50:23Z")

</div>

I do this. I just declare it (my custom class) as an instance variable as part of my AActor derived class and I don’t have the compile issues. However it is problematic if the actor gets re-used or serialised from some reason.

---

<div class="post-metadata">

**Author:** ![Jibera](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jibera](https://forums.unrealengine.com/u/Jibera)\
**Post date:** [September 14, 2016, 12:32pm UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083/8 "2016-09-14T12:32:53Z")

</div>

Can you show me how you did it? Thanks!

---

<div class="post-metadata">

**Author:** ![Jibera](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jibera](https://forums.unrealengine.com/u/Jibera)\
**Post date:** [September 14, 2016, 12:41pm UTC](https://forums.unrealengine.com/t/creating-a-simple-object-in-ue4-c-code-does-not-work/370083/9 "2016-09-14T12:41:06Z")

</div>

I’m trying to implement a certain algorithm. What method would you recommend? Thank you
