I'm reusing a few objects, so when I need them I SetParent and when changing scenes I SetParent back to old one so they don't get destroyed. Code (CSharp): // on ROOT object in scene -> calling leaderboard.BeOnDestroy() here, still calls all // contentAreaObject's OnDestroy()'s private void OnDestroy() { Debug.Log("TEST DESTROY"); leaderboard.BeOnDestroy(); } // but calling leaderboard.BeOnDestroy() here works as expected (without calling OnDestroy // on contentAreaObject's) private void SceneChange() { leaderboard.BeOnDestroy(); SceneSwitcher.LoadScene(GameSettings.INTRO_SCENE); } // a method on a gameObject that is created in 1st scene and never get's destroyed public void BeOnDestroy() { for (int i = 0; i < contentAreaObjects.Count; i++) { contentAreaObjects[i].transform.SetParent(gameObject.transform, false); } } Is there any way I can SetParent in OnDestroy without objects still getting destroyed?
I create 1000 of those objects and bind them to DontDestroyOnLoad on 1st scene, but then I have to reuse them by reparent them to each leaderboard(scrollviews with 'content' <--), and before changing scene I have to reparent them again else they get destroyed because their parent get's destroyed. Which works fine if I manually reparent them before changing scene, but doing it in OnDestroy, even though it still manages to call BeOnDestroy, the objects still get destroyed by the scene.
Organize them on a way that their parent don't get destroyed. There is no way to back off from the OnDestroy.
Redesign so you don't have child objects you wish to preserve attached to parents you are destroying.