Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Update function not being called/freezing.

Discussion in 'Scripting' started by SharksInteractive, Jul 15, 2020.

  1. SharksInteractive

    SharksInteractive

    Joined:
    Jan 27, 2018
    Posts:
    17
    In my game when you get within a certain radius of an object an interaction icon appears. That all works fine except for the interaction.
    I'm going with a hold down blank button to interact sort of thing. In my world some of the object work I can hold down the interaction button and the script runs perfectly fine, but some are sort of frozen. Ill go up to them and they'll be stuck in the middle and wont accept new input and then at random times for a few seconds they will start working again and allow interaction and then they freeze up again. I added a debug.log to their update function and for whatever reason it seems the update function is not running even though others work perfectly fine and I have confirmed they are on an active gameobject and the script is active and there are no errors in the console. Here is the code:
    Code (CSharp):
    1.     using System;
    2.     using System.Collections;
    3.     using System.Collections.Generic;
    4.     using UnityEngine;
    5.     using UnityEngine.Events;
    6.     using UnityEngine.Serialization;
    7.     using UnityEngine.UI;
    8.  
    9.     //A class for making the interact hold circles that appear in the world
    10.     public class holdButtonInteraction : MonoBehaviour
    11.     {
    12.         [Serializable]
    13.         public class ButtonClickedEvent : UnityEvent { }
    14.  
    15.         [FormerlySerializedAs("onClosed")]
    16.         [SerializeField]
    17.         private ButtonClickedEvent m_OnClick = new ButtonClickedEvent();
    18.  
    19.  
    20.         [Tooltip("The key that we should listen for.")]
    21.         public KeyCode keyToListenFor;
    22.  
    23.         [Tooltip("The time you should be required to hold down the key for. [SECONDS]")]
    24.         public float holdTime;
    25.  
    26.         [Tooltip("The image who's fill should be adjusted")]
    27.         public Image iRenderer = null;
    28.  
    29.         //A float to keep track of how long you have held the button/how much to fill the image
    30.         public float time;
    31.  
    32.         // Start is called before the first frame update
    33.         void Start()
    34.         {
    35.             //Make sure the time is reset
    36.             time = 0;
    37.  
    38.             //Make sure the image is valid, if not set it equal to the image attached to this gameobject
    39.             if (!iRenderer) { iRenderer = GetComponent<Image>(); }
    40.         }
    41.  
    42.         // Update is called once per frame
    43.         void Update()
    44.         {
    45.             //Make sure Irenderer is not null (If there is no image object attached to the gameobject)
    46.             if (iRenderer)
    47.             {
    48.                 //Update the fill on the image
    49.                 Debug.Log("Fill adjusted!"); //Debug.Log that is not being called
    50.                 iRenderer.fillAmount = time;
    51.             }
    52.             else
    53.             {
    54.                 //Warn in the console
    55.                 Debug.LogWarning("No image component attached to GameObject or iRenderer component is set to an inccorect value.");
    56.             }
    57.  
    58.             //Check if we have pressed down the key
    59.             if (Input.GetKey(keyToListenFor))
    60.             {
    61.                 //When we are pressing the key increase the value of time
    62.                 //Ensure we do not go over 1
    63.                 if (time < 1)
    64.                 {
    65.                     //Increment the time by 1 / holdtime each frame use time.deltatime to find out how much to increment by
    66.                     time += 1 / holdTime * Time.deltaTime;
    67.                 }
    68.             }
    69.             else
    70.             {
    71.                 //Decrement the time var when not pressed
    72.                 if (time > 0)
    73.                 { time -= 0.1f / holdTime * Time.deltaTime * 3; }
    74.             }
    75.  
    76.             //Check if we have met the time requirement and if so reset the time and invoke the event
    77.             if (time >= 1)
    78.             {
    79.                 time = 0;
    80.                 m_OnClick.Invoke();
    81.             }
    82.         }
    83.  
    84.  
    85.     }
    BTW: I'm on Unity 2019.4.1f1 and am upgrading to 2019.4.4f1 to see if that changes anything.
    Also, I have another monobehaviour on the parent obect of said script and when one freezes they both do.
     
    Last edited: Jul 15, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Nothing in that code will prevent Update from running. Check in your console that you haven't hidden any message types, and also make note of whether "collapse" mode is turned on (as this will make a large difference in how the results will look when the same message is output over and over every frame).

    There's also nothing in your code here that would turn it on and off based on how close the player is standing.
     
  3. SharksInteractive

    SharksInteractive

    Joined:
    Jan 27, 2018
    Posts:
    17
    The off/on is controlled from another object, I just activate and deactivate the game object that this script lives on. All messages are shown and collapse is off.
     
  4. SharksInteractive

    SharksInteractive

    Joined:
    Jan 27, 2018
    Posts:
    17
    Also, I have another monobehaviour on the parent obect of said script and when one freezes they both do.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Is it possible that the entire game is freezing, or is it definitely some scripts and not others?
     
  6. SharksInteractive

    SharksInteractive

    Joined:
    Jan 27, 2018
    Posts:
    17
    Im still able to move and look around.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    When the game is running have you checked if one or both of these GameObjects is deactivated when they are "frozen"? (Also check if the individual behaviours are disabled?)
     
  8. SharksInteractive

    SharksInteractive

    Joined:
    Jan 27, 2018
    Posts:
    17
    I've ensured that the game object is not disabled, and also that the individual behaviours are not disabled. Also the scripts are directly on the UI component so if the gameobejct was disabled the UI would not be displaying as well.
     
  9. SharksInteractive

    SharksInteractive

    Joined:
    Jan 27, 2018
    Posts:
    17
    I've fixed it, thanks to replies on another post on unity answers I was able to fix it, for anyone who is interested here in how I did it here is my response on unity answers,

    "Thanks for the help, I implemented the changes, using != null, toggling via just a if(bool) {return;} instead of enabling/disabling the gameobejct, making sure the image component fill always ends on zero, and cutting down on my comments ;) and now everything is working super smoothly!!! Thanks a bunch!"