Search Unity

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

Discussion in 'Scripting' started by markashburner, Jan 17, 2020.

  1. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    I thought I managed to get around this issue but apparently not?

    Code (CSharp):
    1. public void OnEnable()
    2.         {
    3.             microOrbitPool = GameObject.Find("MicroOrbitPool");
    4.             macroOrbitPool = GameObject.Find("MacroOrbitPool");
    5.  
    6.             if (!microOrbitPool && !macroOrbitPool)
    7.             {
    8.                 var micro = new GameObject();
    9.                 var macro = new GameObject();
    10.                GameObject microPlanetObj = Resources.Load<GameObject>("microOrbit") as GameObject;
    11.                GameObject macroPlanetObj = Resources.Load<GameObject>("macroOrbit") as GameObject;
    12.                microPlanet = microPlanetObj;
    13.                macroPlanet = macroPlanetObj;
    14.  
    15.                 micro.name = "MicroOrbitPool";
    16.                 macro.name = "MacroOrbitPool";
    17.                 micro.transform.parent = GameObject.Find("PoolLocalSystem").transform;
    18.                 macro.transform.parent = GameObject.Find("PoolLocalSystem").transform;
    19.  
    20.                 microOrbitPool = micro;
    21.                 macroOrbitPool = macro;
    22.                 for (int i = 0; i < numberOfPlanets.y * numberOfMoons.y; i++)
    23.                 {
    24.                     GameObject microPlanetClone = Instantiate(microPlanet) as GameObject;
    25.  
    26.                     microPlanetClone.transform.parent = microOrbitPool.transform;
    27.  
    28.                     microPlanetClone.gameObject.SetActive(false);
    29.  
    30.                 }
    31.                 for (int i = 0; i < numberOfPlanets.y * numberOfMoons.y; i++)
    32.                 {
    33.                     GameObject macroPlanetClone = Instantiate(macroPlanet) as GameObject;
    34.  
    35.                     macroPlanetClone.transform.parent = macroOrbitPool.transform;
    36.  
    37.                     macroPlanetClone.gameObject.SetActive(false);
    38.  
    39.                 }
    40.             }
    41.             UniverseSceneController[] usc = Resources.FindObjectsOfTypeAll<UniverseSceneController>();
    42.  
    43.             for (int i = 0; i < usc.Length; i++)
    44.             {
    45.                 universeController = usc[i];
    46.             }
    47.             Initialize();
    48.         }
    49.         public void OnDisable()
    50.         {
    51.          
    52.  
    53.             Debug.Log("On Disable Called");
    54.             macroOrbit[] macro = Resources.FindObjectsOfTypeAll<macroOrbit>();
    55.  
    56.             for(int i = 0; i < macro.Length; i++)
    57.             {
    58.                 macro[i].transform.parent = macroOrbitPool.transform; ///LINE - 168
    59.             }
    60.             microOrbit[] micro = Resources.FindObjectsOfTypeAll<microOrbit>();
    61.  
    62.             for (int i = 0; i < micro.Length; i++)
    63.             {
    64.                 micro[i].transform.parent = microOrbitPool.transform;
    65.             }
    66.  
    67.         }
    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:

    Code (CSharp):
    1.  
    2. if (systems.transform.childCount > 1)
    3.  {
    4.                                      systems.transform.GetChild(0).transform.GetChild(0).GetComponent<CelestialGenerator().enabled = false;
    5.  systems.transform.GetChild(0).gameObject.SetActive(false); ////LINE - 512
    6.  systems.transform.GetChild(0).transform.SetParent(starPool.transform);
    7.                      
    8.  }
    9.  
    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.
     
    Last edited: Jan 17, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    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:

    Code (csharp):
    1. var foo = Instantiate<GameObject>(Resources.Load<GameObject>("MyThingy"));