+1
Searching answer

Attribute UI not updating when using global variable

Sander 7 months ago in Stats updated by Phazorknight 2 weeks ago 9

I've made a simple UI with the stats of the enemy my mouse is hovering over. When using target game object and dragging the object in, the stats work. However, when using global variables, it doesn't. I have a global variable with the game object that was selected. 

Unity version:
2019.2.14f1
Game Creator version:
1.0.7

I think I found the problem. when the attribute UI component is initialized, the target is set at Start(). What I tried to do was change the target in run-time. I've fixed this by making a prefab and instantiating this when the target changes. And destroying the old version of the stats.  

Ok, this doesn't work either. Kinda stuck now. In the end I want to make a stat show in the canvast based on hovering over an object. I wanted to avoid having to make the same amount of stats as I have enemies. Isn't possible to change the source of the attribute UI in run time?

Searching answer

Hi Sander; sorry for the late reply. Changing the variable referenced by the Attribute UI is not possible, but you should be able to change the value of the variable and the Attribute UI should automatically pick the change.

I'm opening a ticket to investigate this further. How you've set up your Attribute UI makes sense and should work just fine. I'll circle back!

+1

Is there already a solution for this? I have the same problem :)

I need my attribute UI set to a global variable so I can show the health of a selected unit.

But now I might have found a way to solve the problem in a more complicated way. But I have to test it first. If it works I will describe it here. :)

+1

In the meantime, I may have found a way to display the numerical values of the attributes of the ever-changing enemies or objects on my user interface:

I assign an empty object to the "attribute UI" in my UI canvas as a fixed target. This empty game object has a "Stat" component.

When I interact with an object, I assign the numeric attribute values to a global variable.

Then I use a trigger that reacts when one of these global variables changes. This then resets the attribute value in my UI canvas with the value of the empty game object.

This is very cumbersome, but so far it works.

This sounds great! Thankyou for sharing. I am going to try getting it to work now.

+1

If it helps, I've also found the actions "sync stat to variable" and "sync attribute to variable" to be very helpful:
If you have an UI element that displays a global variable, it will update once you sync a stat or attribute to said variable with the mentioned actions. (this would be in use with a Text (Variable) component, not the stat UI or attribute UI).