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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Element 'Instance' has no registered factory method <Tutorial UIElements: First Steps>

Discussion in 'UI Toolkit' started by MiguelOscarGarcia, Dec 23, 2019.

  1. MiguelOscarGarcia

    MiguelOscarGarcia

    Joined:
    Oct 19, 2019
    Posts:
    8
    Hi all. I'm trying to follow the (apparently) simple Tutorial UIElements: First Steps and I'm stuck in step 3.
    Using 2019.3.0f3.
    Creating button from code in OnEnable works fine.
    But when creating the uxml files (Assets > Create > UIElements > UXML Template) I come up with these issues:

    a) Visual Studio warning: Element cannot contain white space
    upload_2019-12-23_20-4-16.png

    b) Visual Studio warning: Element Button cannot contain child element
    upload_2019-12-23_20-6-24.png

    c) Visual Studio warning: element UXML has invalid child element Template.
    upload_2019-12-23_20-10-28.png

    d) Element 'Instance' has no registered factory method.
    upload_2019-12-23_20-13-35.png

    Tried with 2019.2, C&P tutorial code explicitly, ... but still not able to get through it.
    Thanks in advance.
     

    Attached Files:

  2. MiguelOscarGarcia

    MiguelOscarGarcia

    Joined:
    Oct 19, 2019
    Posts:
    8
    I managed to get it working just using QuickTool_Main.uxml and avoiding Template and Instance. Then wrote again the original code and it's also working, with ButtonTemplate.uxml and Template and Instance.
    Now it's still highlighting the Template and VisualElement and complaining about the same issues (a, b, c above). But still working.
     
    Last edited: Dec 25, 2019
  3. OxDEADBAAD

    OxDEADBAAD

    Joined:
    Mar 25, 2013
    Posts:
    8
    Hey there!

    Just stumbled upon the same issue here.

    In my case it was a bug with the editor having outdated file templates (I created my files from within the editor).

    I solved it by changing the included namespaces and rulesets in the root node. My layout ended up like this:

    Code (csharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <UXML
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns="UnityEngine.UIElements"
    5.     xsi:noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd"
    6.     xsi:schemaLocation="UnityEngine.UIElements ../UIElementsSchema/UnityEngine.UIElements.xsd">
    7. >
    8.   <Template path="Assets/Game/Editor/Resources/ButtonTemplate.uxml" name="button-template" />
    9.  
    10.   <VisualElement class="buttons-container">
    11.     <Button template="button-template" name="Cube" />
    12.     <Button template="button-template" name="Sphere" />
    13.     <Button template="button-template" name="Capsule" />
    14.     <Button template="button-template" name="Cylinder" />
    15.     <Button template="button-template" name="Plane" />
    16.   </VisualElement>
    17. </UXML>
    In case you're wondering:

    1. I got the correct header from here.

    2. You'll have to change the closing tag from
    Code (csharp):
    1. </engine:UXML>
    to
    Code (csharp):
    1. </UXML>
    3. I haven't found the templates in the editor folder. They're not located with the other templates.

    Hope this helps!
     
  4. PudgeCake

    PudgeCake

    Joined:
    Aug 8, 2019
    Posts:
    4
    I have this issue and changing the UXML tags didn't help. Thus far nothing I've tried has helped: I've tried several different uxml files, some from this tutorial and some building my own and none of them work:
    "VisualElement has no registered factory method"
    "Instance has no registered factory method".

    I'm running version 2019.3.13f1



    edit
    I figured out my issue, I made the same mistake as Stinkfist but missunderstood his solution. I created my uxml files from the in-editor shortcut, same as him, but you don't just change the
    </engine:UXML
    to
    </UXML>
    ; you have to delete everything that the editor generates for you.
    Specifically, delete all of this:
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <engine:UXML
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns:engine="UnityEngine.UIElements"
    5.     xmlns:editor="UnityEditor.UIElements"
    6.     xsi:noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd"
    7. >
    8.    
    9. </engine:UXML>
    And replace it with:
    Code (CSharp):
    1. <UXML xmlns="UnityEngine.UIElements">
    2.  
    3. </UXML>
     
    Last edited: May 23, 2020
    eXiz7 likes this.
  5. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    223
    So I tested both, and both the simplified header in the tutorial and the complete namespaced header generated on right-click > Create work. However as explained in other posts, it really is a matter of consistency: namespacing with "engine:" everywhere or nowhere.

    This really comes from the standard XPath query language: right-click Create generates a standardized XML following XPath specifications, while the tutorial suggests using the minimal version, easier to write on your own. You can merge both by keeping the extra header info but dropping the engine: namespacing (namespacing is mostly useful when working with multiple formats at the same time).

    Another note: xsi:noNamespaceSchemaLocation is auto-generated to match the folder where you created the asset. If you move it later, you should technically update the path by adding or removing ../ so it goes back to your project root, where you will find UIElementsSchema. If I didn't, Jetbrains Rider would underline it in red and help me autocomplete paths properly. But it would still identify UXML classes and auto-complete properly, so I can continue working even without the exact path.
     
  6. crawfis

    crawfis

    Joined:
    Jan 30, 2014
    Posts:
    104
    To clarify this, this is all about namespaces in XML and your code. You should have a namespace for your C# class. This namespace needs to be added to the XML document that is defining your template. You can leave the ui namespace if you want, but add your own:
    Code (Boo):
    1. <UXML xmlns:ui="UnityEngine.UIElements" xmlns:RxGames="RxGames.UI.Gestures">
    2. <RxGames:GestureTutorialContainer>
    3.    <ui:Label text="test" />
    4.  
    In this example, we defined the ui namespace to be UnityEngine.UIElements and use it to access the class UnityEngine.UIElements.Label. We also define the RxGames namespace and use it to access the custom component GestureTutorialContainer, that is within the namespace RxGames.UI.Gestures.GestureTutorialContainer. You can also use the fully qualified name like:
    Code (Boo):
    1. <RxGames.UI.Gestures.GestureTutorialContainer>
    2.    <UnityEngine.UIElements.Label>
    However, often we define a default namespace xmlns="...". This will be added to any element that does not declare its namespace like <Label>, rather than <ui:Label>. This allows us to remove the ui: from everything if we declare the namespace as the default. Anyone know how to override that default and use an explicit namespace?
     
    Fly81 likes this.