UI button triggers debug log but not the actual function

Hello, I have a very specific problem. My button does get clicked on, it does trigger the debug log but the function won’t work. Earlier I just had the “GoRight/GoLeft” attached to it. As it didn’t work, I came up with a variable system. I was pretty shocked to find out the variable also doesn’t change. Both scripts work perfectly fine with A/D controls though, it’s just when it comes to buttons all falls down

Additonal pictures ( can only post one per post as I’m a new user in the Unity forum.


last one

  1. You didn’t show a Canvas in Game View, that could pretty helpful to understand situation.
  2. What is the reason to use Event Trigger at all on the Button? You can use Buttons without it.
  3. You can paste code this way:

Thank you. By using buttons without the event triggers you mean adding code to them connecting them to the main script? Also here’s the canvas, but I strongly believe everything should be okay with it. I’ve already been looking for the problem for a while and changing stuff with the canvas was often on the list of solutions. Doesn’t work for my problem though, or maybe I’m still missing something.

1 Like

Ok, I guess I understood you. You want to hold the button while doing some logic periodically, right?

You need to use other events: OnPointerDown() and OnPointerUp() — instead of OnClick() and OnPointerExit().

I tried a bunch of events. The button is just numb. Triggers only the debug log but that’s it. I tried pointer down and pointer up. Cube still won’t move, not even a centimeter, milimeter, nanometer. Nothing. The variables don’t change when wiring with Force.GoLeftButtonPressed instead of the Force.GoLeft directly.

I also tried adding a new button and it still doesn’t work. How is it even possible? A brand new button

I remember that when I first added the buttons, like a week ago they worked with the OnClick, but I needed the “move when holding the button” effect, so I remade it with another event triggers and it isn’t working ever since. Even when I return to OnClick.

My dear project is stuck, because I officially ran out of options

Code not getting skipped is not a thing. If you see the Debug.Log in the console, then everything before it was definitely executed. (Other than an exception being thrown and stopping execution in a method half-way.)

So there needs to be another explanation why the code doesn’t work as you expect it to. Likely, one of your assumption on how this code should work is wrong.

  • Maybe there are multiple objects and you’re looking at one but another one is logging the message?
  • Maybe the variable somehow immediately gets changed back, so you never see it changed?
  • If it’s a property, maybe something goes wrong inside the setter?

You need to add in a lot more logging, log out the instance IDs to make sure there aren’t unexpected instances involved, or connect a debugger and step through the code. The last one takes some learning and setting up but is often the fastest way to notice where your assumptions don’t match up with reality.

Yeah, without touching a project, it’s hard to say what’s wrong. Try to debug step by step as mentioned by @Adrian using this manual and setting the breakpoints:

I know it may be hard to believe but the script is completely fine. I just added an UI panel, disabled it and made a line of code that sets it back active. Attached the line of code to the Force.GoLeftButtonNotPressed, which gets activated when the pointer leaves the button. It says that the testUI variable is not set (TestUI = the UI panel I’m setting active,) even tho it is. Then I attached the line of code to the A key and by clicking A key the panel does appear, however when the pointer leaves the button, I still get the “variable not set” error.
I know it sounds absurd, same scripts, different triggers. Works with keys, doesn’t work with buttons but at this point I don’t know how is this even possible. Like in the same code, with the exact same. line, one works and the other one doesn’t + it says that the variable is not set.
I have a video but I can’t send it because “new users can’t upload attachments”

I can send you the video or the entire unity file via mail or something. This just doesn’t make sense to me.
I would rather have the code not working at all than working fine unless it’s triggered by a button, then only setting logs