I know this seems convoluted but I have a coroutine which takes a RectTransform of a GameObject as an argument and does things with this RectTransform. I want it to also add the original GameObject to a list of GameObjects. I looked online to see if there was a way to get the owner of a component but to no avail. I really want to keep the argument as RectTransform lest I rewrite my entire script.
Here's a snippet of my code so you can get an idea what I mean:
private void ShowGraph(RectTransform activeGraphContainer) {
graphs.Clear();
graphs.Add(activeGraphContainer.GetComponent<GameObject>()); //<-- My attempt to get the orignal GameObject (doesn't work)
//LABEL Y-AXIS -->
float graphHeight = activeGraphContainer.sizeDelta.y;
float graphWidth = activeGraphContainer.sizeDelta.x; //<-- example of things the coroutine is doing to the RectTransform
This code explains how to find a specific component in a GameObject, assign it to a variable, and find that object (GameObject) from the variable.
GameObject sample;
void Start()
{
// In that object, assign the RectTransform Component to the "rectTransform" variable.
// Find the corresponding object (GameObject) that becomes the parent again in the rectTransform variable and assign it to the sample variable.
RectTransform rectTransform = this.gameObject.transform.GetComponent<RectTransform>();
sample = rectTransform.gameObject;
}
void Update()
{
// Find and use the desired component in the “sample” variable.
float graphHeight = sample.transform.GetComponent<RectTransform>().sizeDelta.y;
float graphWidth = sample.transform.GetComponent<RectTransform>().sizeDelta.x;
}