Engine Crashing on if Statements??

I don’t know why this is happening cause the code works for a while but after the functions get called a few times the engine crashes. Both functions are input functions.
Crashes on these line:

if (LockedOnWidget->GetWidgetClass() != nullptr)

LockedOnWidget is a UWidgetComponent*

if (LockedOnActor != nullptr)

LockedOnActor is an AActor*

Any suggestions or reasons for this would be appreciated.:slight_smile:
Thanks in advance.

Did you try

if (LockedOnActor)

Yes.

1 Like

Strange…

Any suggestions?

Maybe a silly question but are you checking them in the right order?

if (LockedOnActor != nullptr){
   if (LockedOnWidget->GetWidgetClass() != nullptr){
       // extra code here
   }
}

You need to be sure LockedOnActor is ok before accessing it’s GetWidgetClass method.

1 Like

Oh sorry forgot to mention they’re in different cpp files. LockedOnActor* check(see if its been set) is in the character class and is used to access a function in the pawn class that uses LockedOnWidget->GetWidgetClass() check(see if its been set).

UPROPERTY()
UObject *LockedOnWidget;

(garbage collector)

wdym.

Did you declared your UObject pointer using UPROPERTY()?

do it

I did from the start.

The most likely thing is the object containing your comparison is dodgy. What is the this ptr for the object as it crashes? What is the flow to the function that is crashing? as perhaps the object has been GC’d but still being trigger by a timer or somthing.

so… is not the garbaje collector then…

¿Your widget is a blueprint derived from a C++ class?

Maybe it’s not instantiated.

Try to get the class like this:

TSubclassOf< UMyClass > WidgetClass;

And set it using the blueprint


Here’s an example of what’s happening.

You are using LockedOnWidget in two diferent classes… is LockedOnWidget a global variable or…?

Those two classes are different universes… nothing you do in one will affect the other.

Try declaring LockedOnWidget in the GameInstace… then you can access to the same variable in diferent clasess (it will be global)

Its a component on a pawn.

Look into what casting is and then you’ll understand what I’m doing.