Hello,
I’m back to my favorite forum website and looking to get some clarification on a few things please. According to Unity API Image.fillAmount the range for the bar goes 0-1 correct? I’m trying to figure out if this is the case what’s wrong with what I’m managed to type out on my script that causes the fill radial to go straight to fill in the update method, but if I wanted to play around with variations like “Input.GetKeyDown” why isn’t the radial bar increasing? I have all the variables and they are all assigned. but I can’t wrap my head around what’s not allowing the InputGetKeyDown function to work . If someone doesn’t mind telling me where I went wrong or what I need to reference to fix this would be awesome .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnableUI : MonoBehaviour
{
[SerializeField]
private Canvas canvas;
[SerializeField]
private Image button;
[SerializeField]
private Image fill;
[SerializeField]
private bool active;
[SerializeField]
private float fillTimer = 3.0f;
// Start is called before the first frame update
void Start()
{
//Setting active for testing purposes
canvas.enabled = true;
fill = GetComponent<Image>();
}
// Update is called once per frame
void Update()
{
// fill.fillAmount += 0.01f / fillTimer * Time.deltaTime;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
canvas.enabled = true;
active = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
canvas.enabled = false;
active = false;
}
}
public void ButtonPressed()
{
if (Input.GetKeyDown("KeyCode.E"))
{
fill.fillAmount += 0.01f / fillTimer * Time.deltaTime;
}
}
}
When checking for Input, you normally do that with Update. It would be fine where it is if you called ButtonPressed from Update, but you aren’t…
Not even sure where or how you are trying to Call ButtonPressed.
1 Like
Brathnann,
Hey there I wanted to say thank you for taking the time to let me know about checking for input. I didn’t know it’s normal to check for input in the Update function thanks for that info.
Perhaps it’ll become more clear to you why this makes sense when you ask yourself the question: where else would you check for Input? Because it’s not somewhere miraculously done for you, Unity is not smart enough to invoke OnButtonPressed for you when press a key or generate other Input (e.g. with a mouse button). There are, unfortunately some methods in Unity that seem to be magically invoked (start, awake, update, ondestroy, onenable and oncollisionenter are just some of them), but in general, you need to make sure everything happens by yourself.
Here’s a list of the most important ‘Magic Methods’ so you can see what methods and Messages are available, and when they are invoked. If you don’t find a particular method in that list, it won’t be invoked by Unity. Note that ButtonPressed in not on that list.
if (Input.GetKeyDown("KeyCode.E"))
Note that this is only true for one frame when the user initially presses the key, it doesn’t return true while they hold the key down.
You can use GetKey for that, if it’s what you are looking for, though I’m not sure it is.
You also don’t need to pass the keycode argument as a string for either of these methods. It’s preferable not to pass them as strings, as you can make a typo in a string and it will only be picked up and throw an error at runtime. They are also less performant. You get intellisense popup if you don’t use a string as well, so it will give you a list of all the keycodes
if (Input.GetKeyDown("KeyCode.E"))
You can and should write
if (Input.GetKeyDown(KeyCode.E))
The issue with inputs events and Update is that Unity checks and refreshes them just before each Update. If you check some other time you may double-count one, or miss it:
click <click can be detected> <click is gone>
Update1 Update2
E1 (no click) E2 (no click)
E1 (click) E2 (same click)
E1 (click) E2 (no click)
You can check Input.GetKeyDown whenever you want, and it will probably work just fine, as in the 3rd E1/E2 pair But it will also probably mess-up 1 time in 10.