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.