Download

Cast to Actor Blueprint fails

I have a level that has a golf club and a screen. When the player picks up the club and hits a ball, I have a component hit event that I simply want to increment a counter on the screen. So there is the club blueprint (pictured below) and the screen blueprint.

On the event, I do some stuff with the ball like play a sound and set a variable that it has been hit, and that all works. You can see the end of that part in the very left of the picture as it sets the ‘been hit’ variable. After that I try and cast to the screen and update the hit counter, then show it on the screen via the ‘Set Hits’ event. There are no compile errors but when I play it, the cast fails, I see the ‘failed’ message show up when I hit the ball.

Any idea why? The only difference from casting to the ball is that the target for the ball comes out of the hit event, whereas the screen target comes from an object reference called ‘objVideo’. You can see on the right side of the pic how that is set up.

Any idea on why this cast is failing?

I would suggest doing 2 things. 1 check to make sure that the object inside of Obj Video is valid and 2 if the cast fails you may want to check what the type of Obj Video is. It could be possible that the value of Obj Video has no relationship to BP_Video.

IsValid:

Yes isValid does return false, but no surprise considering the cast fails. I don’t know how to see the object type.

Based on the documentation it means that the current value of Obj Video is null or pending kill. So trying to get the the type wouldn’t make much sense since IsValid() is failing as well.

So back to the question, why is it failing…

It’s likely failing because you have not set Obj Video before trying to access it.

Without knowing how Obj Video is being set it’s not something that anyone will be able to answer.

I’ve simply placed in (BP_Video) into the level from the editor. It’s not created at runtime or anything. Only other thing is the object reference settings which I showed in the picture. What else do I have to do? The ObjVideo is just an object variable with the reference. Not sure what you mean about ‘setting it’ other than the settings.

So I’m still new to game development and UE and very much so avoid blueprints myself so I’m not 100% sure on how the Default Value works “it’s not part of the C++ side of things”.

If the value of Obj Video is not set then the default for it would normally be null which would cause it to fail the IsValid() check and also the Cast as we talked about earlier. So my best guess is that Obj Video is never being assigned/set to the object that you want it be. It could be something that you may need to do in the Construction Script or maybe the Default Value section isn’t working as expected. Though I’m not sure why it’s not being assigned/set just that it is.

Just a thought and I’m not sure if this is the best way to do it but maybe a solution. Explicitly assign the object you want to Obj Video in Event Begin Play.

For now I have it working just by using ‘Get Actor of Class (BP_Video)’…but it feels hacky. I know it isn’t the proper way.