How To Change Style Properties At RunTime

Hello,

i’m trying to build a killfeed in which the visual elements should be fading out at some point in time.
I have tried to access the property from my elements like this:

killInfo.style.opacity = killInfo.style.opacity.vaule - someOtherValue;

I use this line of code inside a schduled item (not that this is important).

When I check the Debugger it acutally shows me how the inline opacity value changes but in the game scene nothing changes.

Changing style properties like displayStyle is working! Is this a bug or what can I do better?

Thank you!

Try killIInfo,resolvedStyle.opacity.value

How do you mean?
resolvedStyle.opacity is readonly.

killInfo.style.opacity = killInfo.resolvedStyle.opacity.value - someOtherValue;

resolvedStyle.opacity does not have the property value.
But i have already tried

  • killInfo.style.opacity = killInfo.resolvedStyle.opacity - someOtherValue;

The thing is that the killInfo.style.opacity actually changes but the property never bevomes applied on rendering the VisualElement.

Hello, this sounds like it should work, are you sure you’re watching the correct VisualElement in the Game View? If you see it being modified in the UI Toolkit Debugger, it should be changing in the Game View as well for sure. If it’s not, you should report a bug (Help > Report a Bug) with a simple project showing how we can reproduce it.

Side note though, any reason why you’re not using the USS Transitions instead of controlling the change yourself with a scheduled change? Assuming you’re on Unity 2021.2+, USS Transitions (very similar to CSS Transitions) should be enough to do what you’re trying to do here.

I thank you for your reply! I got it working with both, using transitions and a scheduled item on my messages with ExecuteLater to set the opacity after a certain amount of time to 0.
Setting the opacity right away results in no transition at all even if i added a delay to the transition but i think that would be a normal behaviour.

Yes, you need to let it layout for at least 1 frame before you can have a transition happen, so it has an initial state, and then it will transition to the end state.

Glad you got it working!! :smile:

My God…how is anyone supposed to work with this…Toolkit is an abomination.

3 Likes