- Home /
Can't resolve UnityEngine.UIElements.UIDocument type with UIToolkit
I have been building a simple UI for my game using the UI Toolkit and UI Builder both at v1.0-preview.14. Building the UI was straightforward enough however I seem to be having a bit of trouble interacting with it from C#. Most code examples for the UIToolkit reference the UIDocument then use this to get the root VisualElement - something like what I have done here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class BattleUI : MonoBehaviour
{
[SerializeField]private UIDocument uiDocument;
private VisualElement container;
private Label scoreLabel;
// Start is called before the first frame update
void Start()
{
container = uiDocument.rootVisualElement.Q<VisualElement>("LeftColumn");
scoreLabel = container.Q<Label>("ScoreLabel");
GameCore.OnScoreChangedEvent += GameCore_OnScoreChanged;
}
private void GameCore_OnScoreChanged (object sender, GameCore.OnScoreChangedArgs e)
{
scoreLabel.text = e.newScore;
}
}
It seems that the type UnityEngine.UIElements.UIDocument cannot be resolved. I don't know if this is some sort of namespace conflict or I am missing something obvious, however UIDocument is definitely listed as a class under UnityEngine.UIElements in the UIToolkit docs: https://docs.unity3d.com/Packages/com.unity.ui@1.0/api/index.html . Does anyone know what is causing this?
Answer by RandomComm3nt · Jul 17, 2021 at 10:47 AM
If you are using an assembly definition for your project, you need to add an assembly definition reference to UnityEngine.UIElementsGameObjectsModule.