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

[ExecuteAlways] doesn't work in Unity 2021.3.1f1 (LTS)?

Discussion in 'Scripting' started by KristofferH, May 3, 2022.

  1. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    51
    I have this simple code on a Cube in a newly created 3D template scene in Unity 2021.3.1f1 (LTS), I have also activated Always Refresh in the scene viewport but the cube is only moving when I click the mouse button in the scene view or sometimes when I move the mouse over an editor element (but only somethimes). What is going on here?! This works with no problem in 2020.3.23f1 for instance. Is this a bug or some sort of "performance feature" that I need to disable somewhere? But I can't find any settings for scene view update or refreshing.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteAlways]
    4. public class Demo : MonoBehaviour
    5. {
    6.     void Update()
    7.     {
    8.         transform.position = new Vector3(0.0f, 1.5f + Mathf.Sin(Time.time), 0.0f);
    9.     }
    10. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    You'd be better off using a basic debugging technique to just output a message to the console rather than trying some complex transform time-based update and visually seeing those results. Then just change the transform using some constant and see if it changes. If those work you know it's being called, it updates transforms then you can figure out the rest.

    From what you've said though, it's a repaint thing. Yes, the editor doesn't repaint all the views all the time AFAIK. Same with the inspector window.

    I'm not aware of a change but I'm not part of the Editor team. I'll see if I can find out if there's been an intentional change.

    In the meantime, you can verify it using this to force a repaint per-frame: https://docs.unity3d.com/ScriptReference/SceneView.RepaintAll.html
     
  3. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    51
    Okey, I have replaced the "complex transform" with a Debog.Log(), but it is the same result, if I move the mouse around over the editor UI it sometimes calles update, like once per minute or something like that (I didn't actually time it, I just gave an example of how infrequent it happens) or it is being called when I click the mouse button in the scene viewport. I have been doing these kinds of things in Unity since version 2018, so something must have changed or there is a bug?. Here is the new code I'm using, I'm hoping this one isn't too "complex" for Unity to handle as well.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteAlways]
    4. public class Demo : MonoBehaviour
    5. {
    6.     void Update()
    7.     {
    8.         Debug.Log("Update being called.");
    9.     }
    10. }
     
  4. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    51
    I tried forcing the repaint but it makes no difference.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [ExecuteAlways]
    5. public class Demo : MonoBehaviour
    6. {
    7.     void Update()
    8.     {
    9.         Debug.Log("Update being called.");
    10.         SceneView.RepaintAll();
    11.     }
    12. }
    I've tried with both RepaintAll() above and below the Debug.Log() code line, it makes no difference.

    Interesting side note though: every time I recompile this script the editor deactivates the scene view button "Toggle skybox, fog and various other effects". But whether this option is on or off doesn't matter, the results are still the same.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    I never indicated it was too complex for Unity but rather unneccesarily complex for verifying what your title suggests is happening is all. Setting the transform to (say) Vector3.right would've sufficed. It was an observation and many users, none of which we have any idea of their skill level, often don't isolate what the issue is and use overly complex examples. This is what I was attempting to highlight.

    As I already answered above; I'm not aware of a change but I'll ask. I won't get a reply immediately though.

    https://docs.unity3d.com/ScriptReference/ExecuteAlways.html
    AFAIK the update is only called when something in the scene is changed such as a Transform.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    I don't think the repaint causes the update to be called. I was referring to using it to see your transform changes in your original post.
     
  7. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    51
    I tried [ExecuteInEditMode] instead of [ExecuteAlways] just to check, but it is the same issue.
    Please let me know when you hear from the editor team, so I know whether I need to file a bug report or not :)

    As I said, this has been working for me with no issues, whatsoever, for the past tree years, at least, and I have been using code like this for countless scripts.

    Thanks for your help! @MelvMay :)
     
    MelvMay likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Personally, if you have detected a sudden change, I'd report a bug as a regression immediately anyway regardless of whatever reply I might get! :)
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    So you know, I was curious so I just ran a test in 2020.3.32f1 and 2021.3.1f1 and the results were identical in that you don't get continuous updates in either versions. I was using this code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteAlways]
    4. public class TransformUpdate : MonoBehaviour
    5. {
    6.     // Update is called once per frame
    7.     void Update()
    8.     {
    9.         transform.position = Random.insideUnitCircle * 5f;
    10.     }
    11. }
    12.  
    You only get updates if you click with the mouse or when the mouse enters/exits the view.
     
  10. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    51
    I just tried your code in 2020.3.23f1 and it works like a charm. Magic! :)
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Maybe this will help you too depending on your circumstance: https://docs.unity3d.com/ScriptReference/EditorApplication.QueuePlayerLoopUpdate.html

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [ExecuteAlways]
    5. public class TransformUpdate : MonoBehaviour
    6. {
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         transform.position = Random.insideUnitCircle * 5f;
    11.     }
    12.  
    13.     void OnDrawGizmos()
    14.     {
    15. #if UNITY_EDITOR
    16.       // Ensure continuous Update calls.
    17.       if (!Application.isPlaying)
    18.       {
    19.          EditorApplication.QueuePlayerLoopUpdate();
    20.          SceneView.RepaintAll();
    21.       }
    22. #endif
    23.     }
    24. }
    25.  
     
    misharigot likes this.
  12. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    51
    I tried adding that editor code on a separate gameobject in the scene to handle all scene updates and that seems to work as a workaround.

    Like so:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class SceneRepaint : MonoBehaviour
    5. {
    6.     void OnDrawGizmos()
    7.     {
    8. #if UNITY_EDITOR
    9.         // Ensure continuous Update calls.
    10.         if (!Application.isPlaying)
    11.         {
    12.             EditorApplication.QueuePlayerLoopUpdate();
    13.             SceneView.RepaintAll();
    14.         }
    15. #endif
    16.     }
    17. }
     
    MelvMay likes this.