Trying to access childrens variables from parent

I’m instantiating gameobjects that on creation gets a random variable by their own void start script in their prefab. I now want that variable collected in 1 list in order to make a graph using them later.

My problem is now that I have no idea how to access these variables from a parent. Is there any way I can do this?

You could achieve that by any of the following solutions:

  1. Using Find or GetComponentInChildren:
    If you don’t want to modify the child script, you can access the child components using Find or GetComponentInChildren methods.

    public class ChildScript : MonoBehaviour
     public int myVariable;
     // Other code...
    public class ParentScript : MonoBehaviour
        private void Start()
             // Get the ChildScript component attached to the child GameObject
             ChildScript childScript = GetComponentInChildren<ChildScript>();
             // Check if the ChildScript component is found
             if (childScript != null)
                 // Access the public variable within ChildScript
                 int myValue = childScript.myVariable;
                 Debug.Log("My Value: " + myValue);
  2. Using References:
    One way to access child variables from the parent is by creating public variables in the child script and then setting those variables from the parent after instantiating the objects.

  3. Using Interfaces:
    You can also define an interface that exposes the necessary variable and make the child objects implement that interface. Then, you can access the variables using the interface from the parent. This method is useful when you have different types of child objects with varying variables

Okay never expected it to be this easy. Thank you very much!