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
#How to "save" a gameobject to a json file ?
1 messages · Page 1 of 1 (latest)
i found it
it be "GetGameObjectPath(GameObject obj)"
that works for what i need
nevermind
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
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)
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
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
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.
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
i thought of that but wouldnt that be really hard on performance at start if i have a bunch of objects doing transform.find ? should i do that in a coroutine or something
sorry if my questions are dumb
Coroutines aren't "parallel" – they just let you suspend a function and then come back to them later
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
Ohh
"Find" isn't a great name
I'd have just called it GetChild
the name implies that it searches through its grandchildren as well
well i do need to look in the entire scene in my case so it would be gameobject.find
aren't you going to storing the path to a specific child object?
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
You're going to have to search the entire scene if the object could be anywhere
i mean i can but im unsure how id look for the object at a start using that path
Ohhhh wait
i get it
i am an idiot
"Hard on performance at start" isn't a thing. Players aren't going to care or notice if it takes 0.1 seconds longer to load the game
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
That isn't something that would crash