Objects hierarchy change in runtime help please !

Hi !

Is it possible to change the game objects hierarchy
parent/child/none in run-time by using the available Trigger Action.js (vigorously ripped from 3d person platformer tutorial)LOL

I´m not a coder and I really need this functionality
as I´ve got a potential project funder and the soon-to-be demoed product/game critically needs this function.

So could You kindly provide me the code, or at least nudge me in to the right direction.

The deadline is tomorrow 22.5 :frowning:

Thank You In advance

PS. Yes I know,and I will seriously start to learn scripting, BUT not until the demo is done.

Cheers

The hierarchy is defined by each GameObject’s transform component.

Each transform has a parent property, which is either the transform of the object’s parent or null if it has no parent.

You can change it at runtime by assigning a different transform (or null) to the parent property of each object you want to change.

Thank you !!!

Can I get a some (working) sort of example of it ?

There’s an example in the docs for Transform.parent, but basically it’d be something like this:

Say you have 3 objects in the hierarchy, Grandpa, Daddy and Son. You’ve set up the hierarchy so it goes Grandpa >> Daddy >> Son (in this case >> meanse ‘is the parent of’ in the scene view).

Now at runtime, you want to flip the hierarchy. So your script would go like this:

var grandpa;
var daddy;
var son;

function Start {
   son.transform.parent = null;
   daddy.transform.parent = son.transform;
   grandpa.transform.parent = daddy.transform;
}

(And obviously you’d drag the GameObjects from the scene onto the appropriate places in the inspector for the script)

Now when that game runs, it should flip the hierarchy to Son >> Daddy >> Grandpa.

3 Likes

Thank You Dawvwee , You made my day !!!

This method still work in 2024MAY. Unity 2022.3
Note that when hierarchy is changed those game object’s Transform scale will also change, if they were not 1:1:1 scale in the first place.

public GameObject GameObjectForTesting;
public GameObject TestCube01;
public GameObject FacingInd;
public GameObject Sphere;

// Start is called before the first frame update
void Start()
{
    GameObjectForTesting.transform.parent = null;
    Sphere.transform.parent = GameObjectForTesting.transform;
    FacingInd.transform.parent = Sphere.transform;
    TestCube01.transform.parent = FacingInd.transform;
}

Hi,
Please don’t necro post .
If you want to show appreciation use the Like button.
Thread locked.

1 Like