I have a group of UI elements that I want to persist between scenes. The UI elements are of the only canvas in the scene, so each time before there is a scene change, I unparent the UI elements and make them DontDestryOnLoad. After the scene changes, I reparent them to the present canvas.
My script for the UIs:
public void BeforeChangeScenes()
{
transform.SetParent(null);
DontDestroyOnLoad(gameObject);
}
void OnLevelWasLoaded(int level)
{
Canvas[] canvases = GameObject.FindObjectsOfType<Canvas>();
transform.SetParent(canvases[0].transform);
RectTransform rectTransform = gameObject.GetComponent<RectTransform>();
rectTransform.anchorMax = new Vector2(1f, 0f);
rectTransform.anchorMin = new Vector2(0f, 1f);
}
BeforeChanceScenes is called before another script calls SceneManager.LoadScene
The unparenting, loading new scene, reparenting all succeeded. My problem is that after it reparents itself to the canvas it does not show up in the game or the scene. I can only see it in the inspector.
Still need help :/
– Bob-The-ZealotAre the UI Elements visible in the Scene Hierarchy?
– Bernardo_Olafson