'Find child coordinates and pass them further?

I have a list of objects and a script to show one object randomly. How I can receive the coordinates of the active objects' child, so I can pass them further? I wanna world coordinates be written into x,y, and z.

enter image description here

Here is the script:

public GameObject[] objects;
    public int objNumTrunk;
    public int objCountTrunk = 0;
    public float x;
    public float y;
    public float z;
    public void OnMouseDown()
    {
        objNumTrunk = Random.Range(0, 3);
        objCountTrunk = 0;
        while (objCountTrunk < 3)
        {
            objects[objCountTrunk].SetActive(false);
            objCountTrunk += 1;
        }
        objects[objNumTrunk].SetActive(true);
}


Solution 1:[1]

make a new variable and store the reference of objects[objNumTrunk]. i am assuming that you already have a script on the child objects. now, whenever OnMouseDown will be called, the reference will be updated and you can receive the coordinates of the active objects' child

Example:

public GameObject selectedObject;

Inside your OnMouseDown() at the very end

selectedObject = objects[objNumTrunk];

Now you can access the coordinates using selectedObject.GetComponentInChildren<ChildScript>().coordinates

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muhammad Farhan Aqeel