- Home /
Reaprenting in OnDestroy
When my game starts it loads objects in advance and binds it to a DoNotDestroyOnLoad object. It all works fine but the only problem I'm getting and I have no idea how to fix it is: if a scene needed those objects and I exit that scene, it calls reparent function to save objects from being destroyed...
public void ChangeScene()
{
// This one will work as expected, but here you can see items
// getting deleted in front of you, which looks kind of bad
leaderboard.Reparent();
SceneSwitcher.LoadScene2(GameSettings.INTRO_SCREEN);
}
private void OnDestroy()
{
// This one still normally calls the reparent function and debugging
// it, it reparents it back to old object, but next time I'm getting to
// needed screen it claims it destroyed all of them
leaderboard.Reparent();
}
For god's sake I can't fix it. And I need OnDestroy because it call's it later and looks way smoother than calling it just before changing scene.
Can you ins$$anonymous$$d of posting your solution which may be inherently flawed, ins$$anonymous$$d post what you are trying to achieve.
Well I made a leaderboard, which renders 1000 players, for which it creates needed objects in the first scene and binds them to a DoNotDestroy object. Then each time I want to show the leaderboard, I activate those objects and SetParent to the scrollboard. When I quit the scene, I SetParent those objects back to the old one, but I do this right before calling Scene switch. So you see first deletion of all those objects and then the Scene changes and it looks ugly. So I wanted to move the function to OnDestroy, so that it happens in the background, but it just breaks.
Not quite clear which objects have DoNotDestory set, is it the leaderboard itself? if not why not? Regardless ... does this hide it?
leaderboard.gameObject.SetActive(false);
leaderboard.Reparent();
SceneSwitcher.LoadScene2(GameSettings.INTRO_SCREEN);
leaderboard.gameObject.SetActive(true); //assumes leaderboard has DoNotDestory flag set
Answer by janlovsin · Aug 25, 2019 at 10:38 PM
I have the following function which is called in OnDestroy in the needed scenes.
public void Reparent()
{
for (int i = 0; i < contentAreaObjects.Count; i++)
{
contentAreaObjects[i].transform.SetParent(gameObject.transform, false);
}
}
The OnDestroy is called on the root object that contains the contentAreaObjects, but even after this method is called, the OnDestroy of contentAreaObjects is still getting called. Like what?
Calling the above function manually before OnDestroy works fine. Are the objects already queued to get destroyed when the function is called?
Your answer
Follow this Question
Related Questions
Question regarding Delegates & Events 1 Answer
Once Trackable Library is found, How do I set a permanent Timer on the Augumented Objects ? 0 Answers
Unity 5.4.0f3 (64-bit) Crahed on startup! 2 Answers
Use standard shader on particles meshes 0 Answers
Does the animations done using slice modifier used in 3Ds max show the same animations in Unity ? 0 Answers