Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SharksInteractive · Jul 15, 2020 at 09:36 PM · scripting problem

Update function not being called/freezing.

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:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.Serialization;
 using UnityEngine.UI;
 
 //A class for making the interact hold circles that appear in the world
 public class holdButtonInteraction : MonoBehaviour
 {
     [Serializable]
     public class ButtonClickedEvent : UnityEvent { }
 
     [FormerlySerializedAs("onClosed")]
     [SerializeField]
     private ButtonClickedEvent m_OnClick = new ButtonClickedEvent();
 
 
     [Tooltip("The key that we should listen for.")]
     public KeyCode keyToListenFor;
 
     [Tooltip("The time you should be required to hold down the key for. [SECONDS]")]
     public float holdTime;
 
     [Tooltip("The image who's fill should be adjusted")]
     public Image iRenderer = null;
 
     //A float to keep track of how long you have held the button/how much to fill the image
     public float time;
 
     // Start is called before the first frame update
     void Start()
     {
         //Make sure the time is reset
         time = 0;
 
         //Make sure the image is valid, if not set it equal to the image attached to this gameobject
         if (!iRenderer) { iRenderer = GetComponent<Image>(); }
     }
 
     // Update is called once per frame
     void Update()
     {
         //Make sure Irenderer is not null (If there is no image object attached to the gameobject)
         if (iRenderer)
         {
             //Update the fill on the image
             Debug.Log("Fill adjusted!"); //Debug.Log that is not being called
             iRenderer.fillAmount = time;
         }
         else
         {
             //Warn in the console
             Debug.LogWarning("No image component attached to GameObject or iRenderer component is set to an inccorect value.");
         }
 
         //Check if we have pressed down the key
         if (Input.GetKey(keyToListenFor))
         {
             //When we are pressing the key increase the value of time
             //Ensure we do not go over 1
             if (time < 1)
             { 
                 //Increment the time by 1 / holdtime each frame use time.deltatime to find out how much to increment by
                 time += 1 / holdTime * Time.deltaTime;
             }
         }
         else
         {
             //Decrement the time var when not pressed
             if (time > 0)
             { time -= 0.1f / holdTime * Time.deltaTime * 3; }
         }
 
         //Check if we have met the time requirement and if so reset the time and invoke the event
         if (time >= 1) 
         {
             time = 0;
             m_OnClick.Invoke();
         }
     }
 
 
 }


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 object of said script and when one freezes they both do.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jul 15, 2020 at 11:43 PM

There are several things not clear, at least to me. You said you can interact with the objects when you are within a certain radius. However nothing in this script takes care of any distances. So we would assume that there's another script that is enabling / disabling your "holdButtonInteraction" script on the various instances? We don't know when and how that might happen but it of course has a huge effect on what this script does. If you disable the script Update won't be called anymore and therefore the fillAmount of your image would of course stay where it was when you disabled the script. If you don't disable the script I don't really get how you distance check should work.


Another thing I'd like to point out is when your time reaches the value of 1 you set time back to 0. We don't know what actually happens in your callback. However if the object is still there it would start counting up again.


Another possible issue is your automatic decrement. You will always undershoot the value 0 and end up with a slightly negative value. I'm not sure if the Image fillAmount has any issues with negative numbers but I would generally avoid it.


If you actually disable / deactivate the objects when you are too far away I would recommend to keep them active and enabled all the time and just set a state that the script can check.


ps: You really should reduce your amount of comments ^^. This is widely received as bad commenting style. Stating the obvious is counter productive and might even lead to wrong comments in the long run. Wrong comments are much worse than no comments. Comments that just repeat everything that the line of code does it pointless and just makes it harder to read. I would recommend to have a look at Comment (wikipedia)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SharksInteractive · Jul 16, 2020 at 02:31 AM 0
Share

Thanks for the tips on comments, I will keep that in $$anonymous$$d and give the Wikipedia page a read. There is an external object that activates/deactivates the game object this script is on, I have confirmed in the inspector that the script is still active when it freezes. I was thinking of switching to a != null and will try that when I get back. Interesting point about the image renderer ill make sure to cap that. ill also try just using a bool to set active or not ins$$anonymous$$d of changing the active status of the game object. thanks for the tips, ill get back to you!

avatar image SharksInteractive · Jul 19, 2020 at 12:32 AM 0
Share

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 always ends on zero, and cutting down on my comments ;) and now everything is working super smoothly!!! Thanks a bunch!

avatar image
0

Answer by UnityedWeStand · Jul 15, 2020 at 10:50 PM

Try switching out if (iRenderer) and any similar instances with an explicit comparison with null like if (iRenderer != null) .

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 15, 2020 at 11:04 PM 0
Share

While I also prefer the explicit null check, it doesn't make any difference since every UnityEngine.Object has an implicit boolean conversion operator. So this doesn't change anything ^^.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

236 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Load different render textures into Raw image object 0 Answers

Animation is not running, but value is stuck, if Animatori s enabled! 0 Answers

Extracted from Apk Duplicate Scripts when delete one the show multiple error.(2018.2.8f1) unity version 1 Answer

Phrasing error: trying to make a shop with multiple conditions of ints for unlocking items. handle multiple if statements? 1 Answer

How to run the Coroutine of one script in another using the onMouseDown function ? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges