#How to "save" a gameobject to a json file ?

1 messages · Page 1 of 1 (latest)

barren owl
#

not a really good title but i have an object that occasionaly gets reparented to other objects, i also have a save system in which i can save strings, ints, and floats to a json file, my problem is how could i save what parent my object is under, so that whne i start the game the next time it is still parented under that one and not the default one ?
i hope this makes sense, i need to know how i can "reference" a gameobject with a string or something

#

i found it
it be "GetGameObjectPath(GameObject obj)"
that works for what i need

nevermind

hoary talon
# barren owl ~~i found it it be "GetGameObjectPath(GameObject obj)" that works for what i nee...

What is GetGameObjectPath? Never heard of such method and can't find any documentation for it either. Is that something that you made yourself? If the path is based on game object names, I'm not the biggest fan of that, having some way to index the objects would be more preferable. You can also have two objects have the exact same name in the same hierarchy level too so that could lead to ambiguities

atomic tree
#

I imagine that many different libraries have implemented such a method

#

VRChat's SDK includes a "path between" method, for example

#

(so it's just a coincidence that this method is available)

barren owl
#

i saw it on a unity forum and didnt think through, my bad, it is in fact a custom thing someone made-

#

i didnt test it yet i just assumed it would work

#

i guess my question is still open then-?

#

sorry about that

#

i assumed thered also be a find gameobject by path too but now i dont know

atomic tree
#

yeah, you can use Transform.Find to navigate to a child object

#

so transform.Find("Foo/Bar") looks for a child named Foo, then a child named Bar

quick atlas
# barren owl i guess my question is still open then-?

You cannot save a gameobject to a file in general. Certainly not a reference to it. You need to pick and choose which specific information about it you want.

In this example it sounds like you need some unique identifiers for the objects in your scene. For such a case I would recommend a custom component that has a unique id on it (like a GUID) generated at edit time, and use that to identify the object on the scene.

atomic tree
#

if the path could be totally arbitrary, then saving paths would be the way to go

#

if it's a pre-defined set of 'sockets', then use components

barren owl
atomic tree
#

So that wouldn't really be helping

#

Calling Find a few times is completely fine

#

You may be thinking of GameObject.Find, which searches the entire scene

#

Transform.Find looks at the immediate children of the object

barren owl
#

Ohh

atomic tree
#

"Find" isn't a great name

#

I'd have just called it GetChild

#

the name implies that it searches through its grandchildren as well

barren owl
#

well i do need to look in the entire scene in my case so it would be gameobject.find

atomic tree
#

aren't you going to storing the path to a specific child object?

barren owl
#

i was wondering if i had to use gameobject.find or if there was a way tp remember which object and save that, and not have to look around the whole scene for it again at start

atomic tree
#

You're going to have to search the entire scene if the object could be anywhere

barren owl
#

Ohhhh wait

#

i get it

#

i am an idiot

zealous silo
barren owl
#

i meant i dont want it to crash at start becasue a billion things are going on at once

#

i dont know if i should worry about that or if its not what goes on in the application

zealous silo
#

That isn't something that would crash