Why Dose The Cast Fail?

I’m trying to make a locked door that requires a key to be opened so I have a door blueprint that contains a Boolean called “Has The Key” and a chest on my level that spawns the key item blueprint when opened and when you interact with the key it casts to the door blueprint to set the “Has The Key” Boolean to true but for some reason the cast always fails and I can’t figure out why



I also tried casting directly from the Begin Play event and it just immediately fails when the item spawns

What’s Actor? How did you assign this variable value? Where is the door?

How does the key know which actor you mean?

It’s an actor object reference the parent class of the door


Screenshot 2023-04-25 163537

It’s a null reference pointing at nothing. What if there are 5 doors? How does the editor know which one?

Create a string variable but do not enter any text. What do we have?

We need values.


Gimme 30mins, I’ll show you what I mean.

Tried Making It like this but it gives me an error



alright

the cast always fails and I can’t figure out why

Because the variable has no value. Creating a reference variable does nothing unless you actually define what actor it’s referring to.


when you interact with the key it casts to the door blueprint to set the “Has The Key” Boolean to true

  • the key BP

No need to cast if you we use the correct type.

a chest on my level that spawns the key item blueprint when opened

  • the chest BP

  • we assign value:

Now the chest knows which door from the level we meant, and it can pass that value to the spawned key in screenshot #2.


I wouldn’t script it like this, just following what you needed. Seems overly convoluted to me at this point but… you may know more about what you need further down the line so it may be necessary, ofc. :innocent:

I honestly can’t say that I fully understand what you did but It Worked!
Thanks for your patience with me!

1 Like