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. Dismiss Notice

Feedback Unity Bug? Update Function is not being called

Discussion in 'Scripting' started by lauhonyeung, Mar 8, 2019.

  1. lauhonyeung

    lauhonyeung

    Joined:
    Apr 29, 2017
    Posts:
    29
    I post my code here:
    Update is not being called and LateUpdate works well.
    It's attaching on an UGUI element.

    public class UIFollowWorldPos : MonoBehaviour
    {
    public Transform worldObj;
    public Camera cam;

    void Awake()
    {
    cam = Camera.main;
    }

    public void UpdatePos()
    {
    if (worldObj == null || cam == null)
    return;
    transform.position = cam.WorldToScreenPoint(worldObj.position);
    }


    // unity bug?
    private void Update()
    {
    //Debug.Log("au");

    }

    private void LateUpdate()
    {
    //Debug.Log("aa");
    UpdatePos();
    }
    }
     
  2. lauhonyeung

    lauhonyeung

    Joined:
    Apr 29, 2017
    Posts:
    29
    Here is a gif record when problem happened.
    It seemed like being culled.
    My unity version is 2018.3.0f2
     

    Attached Files:

  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,117
    It kind of looks like your script is getting disabled when you're not looking at the object. I don't suppose you have any kind of code that disabled objects, or their components, when they go offscreen?

    That wouldn't explain why LateUpdate would keep getting called, though. Maybe add the object's name to your Debug, in case there are more than one object of this type in your scene?
     
  4. lauhonyeung

    lauhonyeung

    Joined:
    Apr 29, 2017
    Posts:
    29
    It's only just one script in the scene. I ignore this problem for now by moving logic to LateUpdate.
     
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Might be worth confirming that the GameObject the MonoBehaviour component is attached to is active, the MonoBehaviour is enabled, and it's parent GameObjects in it's hierarchy are also enabled.

    To check this maybe put in LateUpdate:

    Code (CSharp):
    1. Debug.Log("gameObject.activeInHierarchy: " + gameObject.activeInHierarchy);
    2.             Debug.Log("gameObject.activeSelf: " + gameObject.activeSelf);
    3.             Debug.Log("enabled: " + this.enabled);
    It would be a very strange error for this to occur.

    One reason I can think of is that you've baked a occlusion culling map, and the object is being culled when out of view.