Wednesday, February 19, 2020

unity - How to access UI elements in a canvas in UnityUI


I am looking for elements on a canvas (a textfield and a slider); so I can retrieve values and pass them to a function.


But so far I can't figure out how to access them; I can see the methods of the canvas itself, but I can't see the name of the elements that I did add on the canvas.


I have the canvas saved in a variable which is public; then I did place a slider and textfield, connected to text labels; but I can't seem to access neither the label nor the slider or textfield.



Canvas --
|___ Text
| |_____ InputField
|
|___ Text
|_____ Slider

I did plan to access to the text value in the input field doing something like canvas.text.inputfield.text, but this does not work, since I can't see the name of the text field when using dot notation. Same goes for the slider.


public Canvas mainmenu;


void Start() {
mainmenu = mainmenu.Getomponent();
}

public RetrieveData(){
// dot notation does not find sub elements of mainmenu canvas
// only the properties and methods of the canvas itself.
}

How do you actually access elements on a UI canvas, if you can't see them directly, even if they are parented to other elements?





No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...