Crash when Iterating through a TArray whith for loop
I have been following an Unreal Engine course on Udemy and at a point in the course, we were required to get the mass of all the actors that overlap with a certain Trigger Volume(namely PressurePlate). We achieved this by using GetOverlappingActors() member of TriggerVolume and then iterating through each item to get mass.
But when I try to iterate through the members of the TArray(out arg of GetOverlappingActors), the editor crashes on play. The log says that there is an Unhandled Exception is the line with the for loop.
The function Defination -:
The Function call -:
Any help would be appreciated.
EDIT1: The crash report-:
EDIT2: Spelling errors.
Without knowing exactly what is on line 75 of opendoor.cpp, I'm going to guess it's this one:
Here you're assuming that every overlapping actor has a UPrimitiveComponent. If an actor doesn't have a primitive component (which is quite possible) then you'll be trying to GetMass from a null pointer, which would cause the error you see.
So, to fix you could check that the component exists before attempting to GetMass. Quick way is to do something like:
But personally I'd look at trying a different method which is a bit more optimal. FindComponentByClass can be expensive to run multiple times per tick so perhaps using an Interface or a custom Actor class which you can query / filter more directly would be beneficial. It depends on what you're trying to do though.
answered Apr 15 '19 at 02:11 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here