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

Bug YGNodeNewWithConfig is not allowed to be called from field initializer - Unity freeze on Play

Discussion in 'UI Toolkit' started by Komikom, May 9, 2021.

  1. Komikom

    Komikom

    Joined:
    May 20, 2015
    Posts:
    38
    I encountered an annoying bug when using UI Toolkit (1.0.0-preview.14).
    Providing this sample code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UIElements;
    3.  
    4. public class TestScript : MonoBehaviour
    5. {
    6.     VisualElement test = new VisualElement();
    7. }
    8.  
    When scripts are compiled, you will get this error in your console:

    YGNodeNewWithConfig is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead

    I think there are two main issues here.

    1) The first is that unity gives you this error only once. It can be cleared and you will not see it again until you open the scene with the script used inside. Plus this error does not block you from entering the play mode. When you try to enter play mode, Unity will just get stuck, frozen forever. When you force quit unity and reopen your project, you will get Failed to import window layout bug, but that is a whole different problem

    2) This is more of a personal opinion, but it seems like a very bad design decision to forbid using constructor in field construction. Creating a new instance of class should be possible all the time. If not, constructors should be private and creating instances will be done through the factory.

    When this turns into compile error...
    Code (CSharp):
    1. class Foo
    2. {
    3.    VisualElement test = VisualElement.Create();
    4. }
    .. its ok.

    When this turns into compile error...
    Code (CSharp):
    1. class Foo
    2. {
    3.    VisualElement test = new VisualElement();
    4. }
    .. its NOT OK


    The most important thing is the 1) I mentioned, this error should really prevent you from entering play mode.
     
  2. lsegal

    lsegal

    Joined:
    Oct 17, 2015
    Posts:
    5
    I'm seeing this too. Has this not been addressed?

    Specifically, this error will not only freeze the editor, but it will corrupt your entire project and require a near re-import to bring the editor back to a usable state. It's quite the harsh error state to get into with barely any notice for the user (I'm lucky I caught the error in the statusbar).
     
  3. ncerone_unity

    ncerone_unity

    Unity Technologies

    Joined:
    Jan 13, 2022
    Posts:
    32
  4. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    384
    In short though, don't do this, as VisualElements are not serializable, any reference you keep in your MonoBehaviours will be lost on DomainReload, eg: if you auto reload when scripts change.
    Doing so during OnEnable() is much safer.