I have a UI button; when I click it, it activates the associated code. If, however, I do this:
if (Screen == 1.0F) Button_Ok1();
… the program still gets to the routine, but doesn’t activate any code within it. I printed a line to make sure I was getting there, and I get the printout, but none of the other functions (i.e., Locked2.transform.localScale = new Vector3(1.600F, 1.600F, 1F) activate.
I’m capturing the enter button in the Update method. It’s seeing the keypress, it’s getting to the function… why arent the other items activating? They do so when I physically click the button with the mouse, but not when called in the update. I feel like I’m missing something easy here.
Ok, so I changed the variable to an int. The problem still persists.
Inside Button_OK1, I have a print statement. This prints. However, none of the transforms are activating. If I access this function via the button click, it works fine; it’s just when called programmatically that it does not work.
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
I’m already doing that, as I said… although, instead of using Debug, I’m using print. The function is called ONLY when the enter key is pressed inside the Update section … or else when the button associated with it is clicked. Either way, I get the print line. However, I get the transforms ONLY when the button is clicked, and NOT when the function is called from Update.
I’m certain it’s not called anywhere else.
I’m certain that the variable is correct (I print that out as well);
the code you think is executing is not actually executing at all - it IS executing, as I have a print statement at the beginning of it that prints out the instant I hit the enter key.
the code is executing far EARLIER or LATER than you think - it is ONLY executing when I call it from Update or click the associated button.
the code is executing far LESS OFTEN than you think- it is ONLY executing when I call it from Update or click the associated button.
the code is executing far MORE OFTEN than you think- it is ONLY executing when I call it from Update or click the associated button.
the code is executing on another GameObject than you think it is- it is ONLY executing when I call it from Update or click the associated button.
This code (the function Button_Ok1) is attached to the OnClick function of a UI button. When the button is clicked, the code executes properly. However:
// Update is called once per frame
void Update()
{
…
if (Input.GetKeyDown(KeyCode.Return))
{
//Enter
print("Screen: "+Screen);
if (Screen == 1) Button_Ok1();
…
}
}
public void Button_Ok1()
{
print("j: "+j);
if (j == 0)
{
print(“hi2”);
Unknown.transform.localScale = new Vector3(1.500F, 1.500F, 1F);
}
}
j is definitely 0, but the transform does not happen - UNLESS you click the button. The print lines happen either way.
No, no animations, just straight code. I’m changing the localScale of a GameObject to make it visible. Once again, this works fine with the button click, but not with the code - the only code that seems to be executing are the print statements.
Did you set its scale to zero to hide it?? That can also play havoc with your transform hierarchy and I know setting even a single field of scale to zero completely voids your warranty with UI stuff, since the layout tools all throw divide by zero exceptions or barf out NaN,NaN,NaN
It’s Unknown.transform.localScale = new Vector3(0.0F, 0.0F, 1F); to hide and Unknown.transform.localScale = new Vector3(1.5F, 1.5F, 1F); to make visible. Once again, this is working perfectly when clicking the button, but not at all when calling the same routine from Update.
If you have a UI button and that button has focus, if you press Return it will fire… is it the button firing AND this update firing and toggling it back off?
The button does NOT have focus. The code is NOT being called twice, but only ONCE by the Update function. It works correctly if you click the button, but NOT if you push enter.
So what if you also scale some other arbitrary object at that same point in the code? Like just make an Unknown2 and see what happens to it… at least you know nobody else knows about that.
Inside my function, I’m setting Screen = 8; if you hit enter and Screen = 8, it’s activating a DIFFERENT function, Button_Ok7 - which immediately hides the object I’ve just unhidden. So: I need to “debounce” the key. I’m using Input.GetKeyDown … any ideas on why it’s apparently pressing it more than once?
YEAH!! Gotta keep after it, rip tear shred… boogs are boogs, hunt 'em down and deboog 'em!
GetKeyDown() will be true for one frame exactly for EVERYBODY who asks during that frame. Could be ten million different scripts, they will all see true from a single keypress.
If that’s unsuitable then you need to test GetKeyDown() one place and track the intention yourself, then consume it only ONCE, and mark it gone. That would be some kind of input queue arbitrator mechanism.
Actually, I figured out this one as well. Update calls Button_Ok1 … and then returns … and then proceeds to call Button_Ok8. I just need to loop out of Update once it’s done.