- Home /
Accessing children UI elements of the canvas
I want to access the children of a particular canvas in order to later manipulate them (e.g. change their position, etc.). How can I do it?
Answer by JedBeryll · May 21, 2016 at 06:24 PM
Well the canvas has transform too, doesn't GetChild(index) do what you need?
Answer by aLovedHater · May 22, 2016 at 12:16 AM
just like any regular game object. to reference a parent's child use gameObject.transform.FindChild ("ChildName").gameObject if the script is on the parent of the child you are trying to reference.
Answer by CaJerry87 · Sep 21, 2019 at 03:23 AM
Text text = can.GetComponent<Transform>().Find("InputField").GetComponent<InputField>().GetComponent<Transform>().Find("Text").GetComponent<Text>();
//where can = a reference to your canvas component
Debug.Log("text: " + text.text);
go through the transforms and use .Find() to insert the string name of the component THEN GETCOMPONENT< INPUTFIELD> (.Find() returns a transform so you're doing Transform.GetComponent< InputField>) It's long and ugly but maybe this will help you.
Answer by monstrXR · Mar 28, 2020 at 12:51 AM
var canvas = GameObject.FindGameObjectWithTag("s2_canvas");
var carSelectionBtns = canvas.transform.GetChild(0).GetChild(0).GetChild(0).GetComponentsInChildren();
Answer by temistoclesarea · Mar 30 at 01:16 PM
try like this
Text textget = can.transform.Find("Canvas").transform.Find("TextGameObject").GetComponent<Text>();
Your answer
Follow this Question
Related Questions
Move UI object to center of screen while maintaining its parenting 2 Answers
UI Minimap 0 Answers
UI fade with CanvasGroup vs of Image.color.alpha or Text.color.alpha performance 1 Answer
How to convert unity editor GUI functions to something that can be used in game? 0 Answers
Is there a way to make a Texture2D screenshot that ignores the canvas? 3 Answers