"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption"

I thought I managed to get around this issue but apparently not?

public void OnEnable()
        {
            microOrbitPool = GameObject.Find("MicroOrbitPool");
            macroOrbitPool = GameObject.Find("MacroOrbitPool");

            if (!microOrbitPool && !macroOrbitPool)
            {
                var micro = new GameObject();
                var macro = new GameObject();
               GameObject microPlanetObj = Resources.Load<GameObject>("microOrbit") as GameObject;
               GameObject macroPlanetObj = Resources.Load<GameObject>("macroOrbit") as GameObject;
               microPlanet = microPlanetObj;
               macroPlanet = macroPlanetObj;

                micro.name = "MicroOrbitPool";
                macro.name = "MacroOrbitPool";
                micro.transform.parent = GameObject.Find("PoolLocalSystem").transform;
                macro.transform.parent = GameObject.Find("PoolLocalSystem").transform;

                microOrbitPool = micro;
                macroOrbitPool = macro;
                for (int i = 0; i < numberOfPlanets.y * numberOfMoons.y; i++)
                {
                    GameObject microPlanetClone = Instantiate(microPlanet) as GameObject;

                    microPlanetClone.transform.parent = microOrbitPool.transform;

                    microPlanetClone.gameObject.SetActive(false);

                }
                for (int i = 0; i < numberOfPlanets.y * numberOfMoons.y; i++)
                {
                    GameObject macroPlanetClone = Instantiate(macroPlanet) as GameObject;

                    macroPlanetClone.transform.parent = macroOrbitPool.transform;

                    macroPlanetClone.gameObject.SetActive(false);

                }
            }
            UniverseSceneController[] usc = Resources.FindObjectsOfTypeAll<UniverseSceneController>();

            for (int i = 0; i < usc.Length; i++)
            {
                universeController = usc[i];
            }
            Initialize();
        }
        public void OnDisable()
        {
         

            Debug.Log("On Disable Called");
            macroOrbit[] macro = Resources.FindObjectsOfTypeAll<macroOrbit>();

            for(int i = 0; i < macro.Length; i++)
            {
                macro[i].transform.parent = macroOrbitPool.transform; ///LINE - 168
            }
            microOrbit[] micro = Resources.FindObjectsOfTypeAll<microOrbit>();

            for (int i = 0; i < micro.Length; i++)
            {
                micro[i].transform.parent = microOrbitPool.transform;
            }

        }

Unity throws me this error from the code above and points to line 168 in the above code:

Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: ‘macroOrbit’).
UnityEngine.Transform:SetParent(Transform)

Then for this code:

if (systems.transform.childCount > 1)
 {
                                     systems.transform.GetChild(0).transform.GetChild(0).GetComponent<CelestialGenerator().enabled = false;
 systems.transform.GetChild(0).gameObject.SetActive(false); ////LINE - 512
 systems.transform.GetChild(0).transform.SetParent(starPool.transform);
                     
 }

It throws me this errror:
UnityEngine.GameObject:SetActive(Boolean)
UniverseController.UniverseSceneController:Warp() (at Assets/_Scripts/Galaxy/UniverseSceneController.cs:512)
UnityEngine.EventSystems.EventSystem:Update()

Can someone please help? I thought I managed to set the parent of these prefabs correctly? What am I doing wrong?

This happens at runtime.

Thanks.

See where you get the asset from Resources.Load? What you get back, that’s actually the prefab. It is a reference to the on-disk asset. Don’t do anything to that. That’s what Unity is complaining about.

Instead, you should Instantiate<GameObject>() the thing you get back from Resources.Load<>() and do everything with that new reference, the reference returned from Instantiate. You can use the same variable over again if you like. That will actually make a fully-operational instance in-scene you can use.

I generally do something like:

var foo = Instantiate<GameObject>(Resources.Load<GameObject>("MyThingy"));
3 Likes