Need to cast to a specific actor, but the actor gets spawned in from BP

Need to cast to a specific actor, but the actor gets spawned in from BP. Plz Help.

You need to be more specific to get a good answer.

Need to cast to a specific actor, but
the actor gets spawned in from BP.

The best answer with the info provided is to GetActorOfClass - but that’s a bad (got moderated by calling it much worse…) answer. If you spawn actors dynamically, you already have a reference to it - so there should be no need.


Tell us what exactly is needed, where things originate and which actor needs to talk to which actor any why. Then you’ll get a proper answer. Chances are you do not need to cast at all…

So I am using the puzzle template and after a bit of testing I found out that object names go in order like PuzzleBlock3, PuzzleBlock4, PuzzleBlock5, PuzzleBlock6, PuzzleBlock7. So I am making a confusing game where I want PuzzleBlock14 to change the mat of PuzzleBlock2 to a different mat, But I have difficulty trying to cast to PuzzleBlock2.

When you spawn actor then keep reference to it, when you spawn the function return reference to spawn actor, keep it or pass it somewhere else for keeping like GameMode, if you need track spawned actors… then track it, it’s standard practice. with case you giving, put PuzzleBlock in to array in gamemode in order, or even use map to keep if more reliably

Also don’t cast to most specific class, you should cat it to base class with a functions you need, in you don’t need to cast if function you want to call is already in actor class, for example actor destroy. For oyu case i would bake base class for all PuzzleBlock, i would not even make diffrent class for each peace, just make them hold all data needed to identify puzzle piece, depending what you really doing

Wait, are the different puzzleblocks instances of classes named puzzleblock1,2,3 etc. or are they different instances of the same class?

Based on your wording I suspect you have fallen into the trap of thinking that Cast is how you reference specific actors in the level. It is not. Cast just means to access an object or actor as one of the specific classes that it already is. For example a Dog is an Animal, but not all Animals are Dogs, so if you have an Animal reference to an actor named Dog45, you can cast it to Dog class and gain access to the variables and functions that all instances of Dog class have. However, you cannot cast Dog45 to Screwdriver7, because a Dog is not a type of Screwdriver. Casting is not how to find references to specific objects, it’s just how you references you already found, as a specific class, which only works if they are that class or a parent or child class of it.

So you have to either find the specific object you want by narrowing down a GetAllActorsOfClass or similar. to just the one you’re looking for, finding it by colliding or tracing into it, already knowing which one it is, finding by Tag, checking the distance or direction toward it, or some other way.

Thank You I was thinking of something similar, but I didn’t think it would work, but it works thank you