Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Trying to access childrens variables from parent

Discussion in 'Scripting' started by maxx1185, Aug 6, 2023.

  1. maxx1185

    maxx1185

    Joined:
    May 28, 2023
    Posts:
    1
    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?
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    When spawning the child, add the component on the spawned child to a list on the parent. Then you have a list of all the components.

    There are many methods to do this (kurt might reply with a list as well haha)
     
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Get the parent of all the parent objects:
    child.transform.root

    to get the top most object in heirarchy.

    Or just next parent:
    transform.parent


    Or from parent to children:
    Code (CSharp):
    1. body_obj = transform.Find("body").gameObject;
    2. rig = body_obj.transform.Find("metarig").gameObject;
    3. root_bone = rig.transform.Find("spine").gameObject;
    But in order to get the script after getting the gameObject:
    ScriptName script1 = obj.GetComponent<ScriptName>();
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    I do not recommend using .Find
    Stuff like GetComponentInParent or GetComponent(s)InChildren is less error prone.
    Setting everything when spawning is a lot cleaner and more SOLID
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    As far as gameObject.Find(), I totally agree. But when going through a massive hierarchy, like a bone rig from a blender model, as far as I know, there is no better way to get said bone. Like "R.indexfinger.001" or even "spine.007".

    For example:
    Code (CSharp):
    1. spine3 = transform.Find("body/metarig/spine/spine.001/spine.002/spine.003").gameObject;
    Is perfectly fine since it goes through the hierarchy of transform.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    It isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

    This can help you keep things tidy when you're instantiating stuff:

    Factory Pattern in lieu of AddComponent (for timing and dependency correctness):

    https://gist.github.com/kurtdekker/5dbd1d30890c3905adddf4e7ba2b8580