Probably very simple question, but… sorry, my abilities are limited:)
I have simple tree of UI elements (see picture) - just background (Bg Visual Element) containing row of buttons.
I would like to get them into the array for easier manipulation in the script. When I do it like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class contrSc : MonoBehaviour
{
...
Button[] kosButAr;//array ready for the buttons
...
void Start()
{
var uiRoot = GameObject.Find("UIDoc").GetComponent<UIDocument>().rootVisualElement;
kosButAr = new Button[ 7 ];//redefine the array
//probably not too smart way how to get access two levels deep into the hierarchy; should be possible to do it in one line?
VisualElement.Hierarchy rootHiera = uiRoot.hierarchy;
VisualElement veBg = rootHiera.ElementAt(0);
VisualElement.Hierarchy veBgHiera = veBg.hierarchy;
//place the buttons into the array
for (int i = 0; i < kosButAr.Length; i++) {
kosButAr[i] = veBgHiera.ElementAt(i);
}
...
I get the console error message “…error CS0266: Cannot implicitly convert type ‘UnityEngine.UIElements.VisualElement’ to ‘UnityEngine.UIElements.Button’. An explicit conversion exists (are you missing a cast?)”
As I am not too much into programming - how to solve the error?
And also - just curious - is it possible to access the second level in the hierarchy somehow easier?
Thanks in advance and apologize for such probably trivial question…
EDIT: At least solved the traversing through the hierarchy (of course quite logical):
...
VisualElement.Hierarchy veBgHiera = uiRoot.hierarchy.ElementAt(0).hierarchy;
...