ScriptableObject to create List<> inside elements of Lists<>?

Hi!

Is there a way to create a custom hierarchy by script in Unity?

Today I am using the hierarchy from the normal way positioning GameObjects as Parent or Child in the Hierarchy window.

But my object has many parts and my hierarchy is getting confusing.
And to help, sometimes I need to back and change one object and as I have many animations, All the animation loose the path. Ok to fix I can go to each clip and fix manuality the path. But it is consuming time.

Hierarchy is basically List<> inside List<> of gameobjects lists…

If would be possible to keep these parts without hierarchy in Hierarchy window but create this hierarchy just managing theses lists, will be nice.

Ofcourse that we need to do with maybe ScriptableObject because to create this lists manuallity in script mode will be probably to much complex. But in the Editor mode just clicking in a button and add a new list in the selected Element of a List will be fast and simple.

Thank you.

You can create any hierarchy you want in script by using Transform.SetParent()

var cube = GameObject.CreatePrimitive( PrimitiveType.Cube);
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.SetParent( cube.transform);
1 Like

9502606--1338568--upload_2023-11-30_14-44-57.png
My english doesnt help so sorry.

My problem isnt to change in realtime.
I thought using SO or something like it, to create in Editor mode one hierarchy by script. So if I need to change it, I can do without losing reference in all animations that I have. Everything in Editor mode. And access this hierarchy by script when necessary in RunTime.

But I guess that is not make sense because also in runTime how I will get the tree of a clicked object? Will need to do a foreach in each entry in these many lists untill to find the object there and so get the tree… Will be slow…

Today I need to find all the tree of a selected object in runtime. I do a simple loop in the parents. So is easy and fast.

So probably it is nor a good idea.

This guy created something like it but for different application.

I mean the transform-heirarchy is effectively a tree-like data structure. It’s pretty easy to roll your own and have it editable in Unity. But, it will require a decent degree of custom editor tools to make it possible.

The problem with using scriptable objects, is that with tree-like structures there’s generally the expectation that each part of the tree has a reference to it’s parent, like we do with transform.parent. This gets messy with scriptable objects as they’re just assets, and any number of other assets could be referencing it.

So to that end you could just emulate the data structure in pure C# code. You can make it natively serialisable in Unity too with [SerializeReference] allowing for cyclical references. This is what I did with my current project, where you pay a robot that you can customise your parts for. The whole player state is represented by a pure C# object called the PartsTree, made up of a tree of Parts. It’s completely serialisable and can be written and loaded from disk, and is used to generate the actual runtime game object representation.

But I don’t think this will really solve your problem. You’ll just be moving the burden of maintenance from one system to another. And it’s a lot of work to bridge the gap from pure C# to Unity’s game objects. A lot of things need to be hooked up via code at runtime, as you can’t hold onto references of anything that lives in a live scene.

I would really be looking at ways to strengthen your current setup before you go down massive rabbit holes like this one.

1 Like

Your answer is really helpful Sr. Really thank you.

You understood exactly what I wish and like you said, both methods sounds not easy to keep. So ok, I will keep the basic method. I don’t have enough knowledge to go so deep. But I will keep it in my mind. Because when we implement something like it, we can go further and for example we can maybe implement a method that can do better then transform.parent and for example get selectedModel.tree(); and take all the selected object tree.

And off course in my case, all changes in hierarchy dont need to be fixed in the clips…

Thank you!

1 Like