How to set an object reference of an actor that is spawned in?

I have this object reference for a carrot that I want to use so I can use the variable has carrot, but I got the error that it accessed None, so I set it up to only work when valid but now it does nothing because it is still none.


I know I have to set the reference here, but the carrot object is spawned in the game when the player presses a button, so I can’t select it because it doesn’t show up in my map until later, is there a way that I can go about either selecting it, or changing the code so I can avoid this issue altogether?

Where do you do it? Inside a widget? In another BP after a key press?

Hi, is this screen from your player blueprint?

Somewhere in your BP you spawn carrot. Return value from that spawn node is the actual reference to that single Carrot Actor. So you can set it there, it would look more or less like this:

Yes it’s here in this widget blueprint!

Thank you! How would I then be able to use this variable in the other blueprint?

Assuming the script you’ve shown is inside the player character: Get Player CharacterCast To your character, set Carrot Reference.

Thank you! How would I then be able to use this variable in the other blueprint?

Avoid pulling data out of widgets, it leads to some bad design choices in the long run.


A neat way to do it all is via a dispatcher that you register once when the game starts. And every time a button is pressed, a reference is set automatically. For this, however, we’d need to know, where, when and how the widget gets created. Tbh, there is generally no reason to even spawn the carrot in the widget… I’d dispatch a click and spawn it where it needs to be referenced.

Thank you! I’ll try that

Ah well that sounds a lot more complicated than I anticipated, and the script I showed is actually inside another actor in the scene that the player can interact with. The widget gets created when the player interacts with a fridge (this script is inside the fridge actor)

Does the fridge spawn carrots only or all kind of stuff? The buttons in the widget the fridge created will, right?

The fridge can also spawn potatoes, but only if you press the potato button, and the carrot button is the only thing that spawns carrots

Which is also here, right?

Or in another widget?

Yes they are found in the same widget!

Here’s how I’d tackle it to make it a bit more futureproof:

  • in the fridge widget we decide what each buttons spawns, any variety of items. Even cake. :cake:

:point_up: Note the ED_ActorToSpawn Event Dispatcher in the bottom left corner. Pressing a button calls it.

  • in your previous thread, the player could only have 1 item at a time; assuming this is still the case, the player can spawn what the fridge produces:

You can query the spawned item with an interface to see what type of foodstuff it is in case it matters.

  • and the fridge interaction is hooked up like so:

Which translates to: whenever a widget’s dispatcher is called, trigger a custom event in the player BP


Good luck!

However, if you want to stick to the method you currently already:

Admittedly, this will be a short lived solution once things get more complicated and a major source of banging one’s head against the wall. On the other hand, it you keep things really simple, it might be fine if a wee bit tedious to maintain.

Thank you! I’m trying this at the moment, sorry for the following, stupid questions, for the SpawnItem custom event, would this be a custom event or when the player interacts? And in the fridge set up, is the variable from promoting Return Value to a variable?

Just a regular custom event that something else will call. This event will listen to the broadcasts of the widget buttons.

And in the fridge set up, is the variable from promoting Return Value to a variable?

Right clicking data pins gives you an option to automatically create a variable of the correct type. Rather than clicking 6 buttons and finding stuff on a long lists, you can promote it:

But you can, ofc, create the variable manually. It’s just a lot of clicking.

Thank you! I’ve got the new script set up now, but I’m running into an issue as when I cast to the player it says it does not inherit from player controller and the items are no longer spawning in

Did you use the correct node?

1 Like

Ah no you got me there😅