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.
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 onm ouseDown()
{
objNumTrunk = Random.Range(0, 3);
objCountTrunk = 0;
while (objCountTrunk < 3)
{
objects[objCountTrunk].SetActive(false);
objCountTrunk = 1;
}
objects[objNumTrunk].SetActive(true);
}
CodePudding user response:
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 onm ouseDown 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 onm ouseDown() at the very end
selectedObject = objects[objNumTrunk];
Now you can access the coordinates using
selectedObject.GetComponentInChildren<ChildScript>().coordinates