Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Code works in Update() but not anywhere else

Discussion in 'Scripting' started by Quincey, Mar 2, 2020.

  1. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    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 :confused:. If someone doesn't mind telling me where I went wrong or what I need to reference to fix this would be awesome :cool:.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class EnableUI : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private Canvas canvas;
    10.  
    11.     [SerializeField]
    12.     private Image button;
    13.  
    14.     [SerializeField]
    15.     private Image fill;
    16.  
    17.     [SerializeField]
    18.     private bool active;
    19.  
    20.     [SerializeField]
    21.     private float fillTimer = 3.0f;
    22.  
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.         //Setting active for testing purposes
    28.         canvas.enabled = true;
    29.         fill = GetComponent<Image>();
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35. //        fill.fillAmount += 0.01f / fillTimer * Time.deltaTime;
    36.     }
    37.  
    38.     private void OnTriggerEnter(Collider other)
    39.     {
    40.         if (other.gameObject.CompareTag("Player"))
    41.         {
    42.             canvas.enabled = true;
    43.             active = true;
    44.         }
    45.     }
    46.  
    47.     private void OnTriggerExit(Collider other)
    48.     {
    49.         if (other.gameObject.CompareTag("Player"))
    50.         {
    51.             canvas.enabled = false;
    52.             active = false;
    53.         }
    54.     }
    55.  
    56.     public void ButtonPressed()
    57.     {
    58.         if (Input.GetKeyDown("KeyCode.E"))
    59.         {
    60.             fill.fillAmount += 0.01f / fillTimer * Time.deltaTime;
    61.         }
    62.     }
    63. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    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.
     
    Quincey likes this.
  3. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    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.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    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.
     
  5. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,303
    Code (CSharp):
    1. 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.
    https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
    You can use GetKey for that, if it's what you are looking for, though I'm not sure it is.
    https://docs.unity3d.com/ScriptReference/Input.GetKey.html
    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

    Code (CSharp):
    1. if (Input.GetKeyDown("KeyCode.E"))
    You can and should write
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.E))
     
    Last edited: Mar 2, 2020
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,926
    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:
    Code (CSharp):
    1. click       <click can be detected>   <click is gone>
    2.       Update1                              Update2
    3.  
    4.   E1 (no click)                                         E2 (no click)
    5.               E1 (click)             E2 (same click)
    6.                      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.