Hey, so I have been working with unity for a little while now, and there is something that I can’t figure out how to do.
I have some objects which have joints that can break and get separated, and In my game I need a way to reset them back to their original state without reloading everything.
It would be nice If the object could do it itself, but its not 100% necessary. I have tried a bunch of different things. I have tried Instantiating new objects based off the object/prefab/other copies of the object and deleting the original, but none of these seam to restore the object. It always just recreates the already broken object.
If anyone has any tips they would be greatly appreciated.
When it joint breaks, it can’t be reset, so it is effectively removed from the system. You could use GameObject.AddComponent to add a new joint, but you need to position the joined object in the right place and also set the connected body of the joint to attach it.
right, recreating all of the broken joints isn’t really a good option. Is there any way at a minimum to reload the object from the prefab?
The only solution I found that works, is creating a controller script, that on start creates a copy of each object that I need to reset, and sticks it behind the camera(its 2D so the objects will never be seen). Then when I get a call to reset I delete the object, and create a new one based off the copy I made that’s behind the camera.
This will work for the time being, but It is also not a very good solution, I have to much with things that are physics objects so they don’t fall, and light sources can become a problem.
Is there anyway I can create a copy of the object in memory(not in the current scene)?
I’m trying to find something better than my current implementation, but can’t come up with anything.
Dusty and I are working together on our first Unity project, and have come up with a few different ways of handling resettable objects. All of them are problematic for one reason or another.
This need must come up for other Unity users from time to time, and I have to imagine other people have solved it before us, possibly in a better way.
What we’re doing right now is attaching a script to all of the “resettable” objects. When the level is loaded, a reset manager script finds all the resettable objects and creates clones of them, off screen. Their kinematics and collisions are disabled.
When the user passes a checkpoint, we remember the checkpoint. When they reset their character, they are moved to their last saved position. At this point, the reset manager destroys the resettable objects that are on screen and instantiates new ones from the clones we created.
We’ve tried other implementations, but nothing seems to catch all the possibilities. Some of the resettable objects might be jointed. Others might be destroyed by the character entirely. The simple ones can just be moved back into position, but the complicated ones require either special handling, or our brute force clone method.
What I don’t like about our current implementation is that it seems messy and wasteful. It does seem to work, though, and I’m willing to leave it alone.
Another way to handle the joints thing might be to create a prefab consisting of an empty object containing the two joined objects as children. If you were to instantiate that prefab, the joint should be connected correctly. You could then un-parent the children by setting both their transform.parent values to null. Alternatively, you could leave them as children and just ignore the parent object.