How I can use NewObject to get and set variable exists in another class ?

I have a class like this

File1.h

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class My_API UFile1 : public USceneComponent
{
	GENERATED_BODY()

public:	
	UFile1();

	bool mykey = false;

In another class for example UFile2 I need to get and set this boolean value how I can use NewObject to do that ? I use Unreal 5.0.3

I am not sure but you can place the bool in the game instance and then call it from whatever place (for example set bool in bp a and read in bp 2)

Hope it helps

Not sure where NewObject comes into play.

What are you trying to do?

Get read/write access to this member variable: mykey from another class

File1 parent is USceneComponent while File2 parent is UBoxComponent

I tried like this

myclass = NewObject<UFile1>(this, file1->StaticClass());

it worked and I can read the inital value but because it is different instance I can not read new values changed by class UFile2 so is there away to read the new change ?

Still don’t get how NewObject would get into play while trying to do this.

If you are trying to get access to data of one component to an other component on the same actor use the Owner.

GetOwner()->FindComponentByClass<UFile1>()->mykey

if you are on another actor and you want to get access use
OtherActor->FindComponentByClass<UFile1>()->mykey

if you have multiple UFile1 components on one actor then it becomes more complicated and you need to identify the component somehow,
either by name or tag

1 Like

was a suggestion from another person, seems C++ new can not do it ?

Yes this work. thanks