Games crashes when calling a function from another actor class

Hello,

When I call a function of an actor by another actor, my game crashes. Let’s say actorA call a SetActorTickEnabled() function of actorB and my game crashes.

Thank you

Are you sure your pointer is really pointing to an actor? How have you set the pointer to point to the actor? If this script is being used on a Blueprint class, you probably aren’t able to point to an actor unless it’s being used inside a scene and the pointer is being altered inside the scene.

If you’re trying to set the pointer using BP nodes, you should set it to BlueprintReadWrite.

If you’re straightly using the class, then I’m guessing that the class is inheriting from AActor as you said it’s an actor, remember to assign the actor through engine’s internal scene combo-box.

If it’s something else, maybe share the error as well and we’ll find it out but I’m mostly sure that your pointer is nullptr.

Could BackAndForthActor be nullptr at any point? Maybe wrap the call in an if statement.

Thank you for your reply.
Yes it’s a null pointer, is there a way to fix it?

Thank you for your reply. Yes it’s a null pointer, is there a way to fix it?

Are you setting it somewhere else in code?

No, I have only declared the pointer to ABackAndForthActor class, like this:

ABackAndForthActor * BackAndForthActor;

that’s it, in the cpp file I use BackAndForthActor->function() to call a function and it crashes

So that line: ABackAndForthActor * BackAndForthActor; just declares a pointer to an instance of ABackAndForthActor, you need to spawn on somewhere and get a reference to it.

You can either do this in this class somewhere using GetWorld()->SpawnActor

Or you could spawn it somewhere else / put one in the level in the editor and get a reference to it, eg with something like UGameplayStatics::GetActorOfClass

You should set it to point to an actor in the engine or from another code if you are pointing to an actor. Drag and drop an instance of your actor into your preferred level and under your script menus, you’ll see some drop menus that will show actors to you. From there, select your preferred actor.

If you’ve made a BP instance out of your class and you’re trying to set the pointer using BP nodes, you should set the actor’s UPROPERTY to BlueprintReadWrite. The , you’ll be able to say Set BackAndForthActor and then you’ll be able to set your pointer up.

There are so many ways to setup your pointer.

You can either get your actor instantiated through code that you can either instantiate using a custom AActor class that you have created, or you can get a reference to one of your actor assets by using the normal UPROPERTY(EditAnywhere) AActor* ActorAsset.

If you already have it in a level, drag and the current actor class to the level you want and select your script. From there, you can use the drop-down menu to point to the actor you have in your level.

If that actor exists as a ChildActorComponent in a blueprint class and you want to setup your pointer in a blueprint class, you can also use the blueprint nodes to setup your pointer by adding BlueprintReadWrite to your BackAndForthActor's UPROPERTY. Then you can use the Set BackAndForthActor node to setup your actor.

As I said, there are many ways to do this. See which you’d like to use.

Because : the class variable “Illegal TEXT reference to a private object in external package”
Very Sufficient way to solving this
Use UPROPERTY(EditAnywhere) TSoftObjectPtr<ABackAndForthActor> BackAndForthActor;
Chance class variable Pointer* to TSoftObject<>

Remember its Work when
1: ABackAndForthActor Actor exist on Map (ABackAndForthActor != NULL)
2: Declare/Set the Actor on BP UPROPRTY() //Select on Map or BackAndForthActor = Cast<ABackAndForthActor>(UObject::GetClass());