Level Variant inside a function

Hi!
I am trying to change the material with a Level variant sets. It works completely fine like this:
image

But I want to put it inside a function instead. Basically when pressing the key “2” I want it to call on a function that has the variant change inside it.
The problem is that it does not work when I do that. Does anyone know why it doesn’t work?