Widget Reference Problem

Hi, so here’s my problem:

I create an image variable on a widget (Icon Widget) and tried to put it on another widget (Button Widget) by making a widget reference. My goal is that when the mouse is hovered on the button, the image brush from this specific button is added on the icon. The problem is, when I hover the mouse, it does’nt do anything and creates an error. After many tries, I realized that the problem is from the Icon widget reference on the Button widget.

It would be really appreciate if someone coud help me find a solutio. Thanks in advance :slight_smile:

1 Like

Hi!

The error states “accessed None trying to read property PreviewWidget”

Are you setting that reference to the PreviewWidget anywhere so the button widget with the on hover event has access to it when you want to use it? The reference needs to be set somewhere before you start using that reference elsewhere.

For example, the widget gets created at some point and added to the viewport (I’m not sure where this happens in your code - maybe on the player controller - maybe on the game mode - maybe on the button widget). At this time you want to save that instance of the PreviewWidget to your variable. If it’s on another blueprint class, you’ll have to cast there first to access it.

Please mark this answer as accepted if it has helped you. If you need more help in this matter, please comment below with more information about the widget and where you’re setting that reference and I’d be glad to assist.

Happy developing,

Reishi

Ok so, I’ll try setting that reference in the PreviewWidget, but I don’t know how to do that. Could you maybe clarify a bit more about it?

Thanks for the fast respond by the way :slight_smile:

Same deal - wherever you’re creating the widget.

In this case, as I understand it, you’re creating both widgets in the MainMenuWB event graph by dragging them into the hierarchy.

Creating the main menu widget in the level blueprint is going to complicate this for you. You’d have to use event dispatchers to pass a reference of the main menu widget to the button widget… Finding somewhere to bind it and call it… seems like a mess that I’m not going to get into. I’m going to assume you’re not familiar with event dispatchers, otherwise that’s likely what you would do if its quite important for the widget to live on the level blueprint.

I think it’s generally a bad practice to manage UMG from the level blueprint. I could be wrong. That’s for level stuff. Menus appear for players, or at the end of a round, stuff like that. So the player controller or game mode would be a better place to create that main menu widget. From either of those you can get a the game mode or player controller in any blueprint and cast to your specific class and access that widget.

creating a main menu in the player controller:

Accessing that and the preview widget in its hierarchy from the buttonWidget:

You’re welcome :slight_smile:

So in order to show the widget on the screen you probably had to use the nodes “Create Widget” and “Add to Viewport” right?

In my example here, the ButtonWidget is getting created and added to the screen from the game mode. For this example, its not relevant where that happens. I created it on the game mode at beginPlay - You likely have this widget created at some other point, like from the player controller key event or something.

The following screenshot is from a simplified version of your ButtonWidget event graph. I’m creating an instance of the PreviewWidget on Event Construct, setting that reference as a variable, and then adding it to the viewport.

So after you create the PreviewWidget, you need to save its reference in the variable so it can be used later:

If you just make a variable of type PreviewWidget, you will still have an empty reference because the variable needs to point to an instance of the widget. Again, it doesn’t matter where this happens as long as it does happen.

So wherever you are creating the widget, the Create Widget node returns a reference to that instance of the widget blueprint. That’s where you need to set the variable.

Happy developing,
and please mark the answer above accepted if this has been helpful. If you still have questions, let me know!

Reishi

Ok, but how do I do that if I added The PreviewWidget and the ButtonWidget is inside another Widget (Called: MainMenuWB) ? The MainMenuWB is created inside the level blueprint, so its this one that shows the other 2 widget (PreviewWidget and the ButtonWidget)…

Omg thank you so much, I manage to do it by switching where I created the Main widget. Instead of putting it in the Level blueprint, I putted it on the player character and it worked!

Thank you again!!!

Glad I could help :slight_smile:
Happy developing!