1
votes

I wanted to get data available in root canvas from the child/member of the canvas. It's possible to do it using this https://docs.unity3d.com/ScriptReference/Canvas-rootCanvas.html but it is unusable if this.gameobject does not have canvas component and adding canvas component is out of option , and manually assign the gameobject from unity editor is also out of option because it close the door for automation.

So my question is , how do i get the root canvas gameobject from its child without using this.gameObject.GetComponent<Canvas>().rootcanvas ? I also have tried collider.transform.root but it fails if the root canvas is not in the top hierarchy.

1
I think your question might be based on a false assumption. It isn't possible to have visible UI that isn't ultimately underneath a Canvas. If you do have a UI object that doesn't have a root canvas, it won't be visible.Joseph
@Joseph yes but to find the root canvas of the UI object , i need to use this.gameObject.GetComponent<Canvas>().rootcanvas , and if my gameobject does not have canvas component , i can't find the root canvas using the canvas.rootcanvasi'm ashamed with what i asked

1 Answers

3
votes

Try GetComponentInParent<Canvas>().rootCanvas. This assumes you have a valid, visible UI element. Any UI element you can see must have a Canvas higher up in its hierarchy, and then you can get the rootCanvas of that canvas.