Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

AnswerHub Maintenance

Background maintenance is scheduled to occur between 9 - 11am EDT on Tuesday, May 21. Site operation may be slower than normal during this time and a brief interruption in operation may be observed

Cannot add a Component to an Actor

In my Actor subclass ATrack, I have a method that takes in a TSubclassOf(UPiece) called NextPiece. UPiece is derived from StaticMeshComponent.

Here's my code:

 void ATrack::ArrangeNextPiece(TSubclassOf<class UPiece> NextPiece)
     auto CurrentPiece = NextPiece;
     FString CurrentPieceName = CurrentPiece->GetName();
     UE_LOG(LogTemp, Warning, TEXT("CurrentPieceName: %s"), *CurrentPieceName)
     UPiece* MyTrackPiece = NewObject<UPiece>(CurrentPiece)
     UE_LOG(LogTemp, Warning, TEXT("MyTrackPiece: %s"), *MyTrackPiece->GetName());
     // MyTrackPiece->SetWorldLocationAndRotation(NextPieceLocation, NextPieceRotation, false);

This runs fine and logs out this: alt text

But the piece does not appear in the hierarchy, and if I uncomment the SetWorldLocationAndRotation() method, it hard-crashes.

If I change the NewObject line to this...

 UPiece* MyTrackPiece = NewObject<UPiece>(this, CurrentPiece)

...then it hard-crashes.

It seems that the piece will be created happily enough, but my ATrack actor refuses to accept a new component?

Product Version: UE 4.20
capture.png (7.9 kB)
more ▼

asked Sep 08 '18 at 04:49 AM in C++ Programming

avatar image

6 3 2

avatar image Travis.D Sep 09 '18 at 03:09 AM

No suggestions? I feel like this should be easier than it is. Is NewObject even the correct function? It seems like it does nothing.

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

First I highly suggest not using Auto for this, it only servers to confuse you in debugging. The correct form of NewObject should be:

 UPiece * MyTrackPiece = NewObject<UPiece>(this, CurrentPeice);

This is what I use everywhere. You say this is crashing on you, than run Visual Studio in Debug Mode, the Hard crash should trigger a break point. Use the stack trace to hunt down the problem. Hard crashes are usually caused by the object you are calling the function on being null ("this" probably is null).

After creating the peice you need to Attach it to the actor using


Also don't forget to register your component. If you are doing this with a Server In mind you run the function on the server only, and make sure replication for your component is turned on.

more ▼

answered Sep 09 '18 at 05:43 AM

avatar image

454 32 25 55

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question