I created a canvas called “Information” to display application related information on a child Text element. However later I wanted to disable it on run time after certain time as the application state changes. I used the following lines to do this.
Canvas uiPanel = FindObjectOfType();
uiPanel.enabled = false;
Despite its Text element was gone, canvas was still there with just a uniform color. The result was same when I called destroyobject function to destroy canvas, again text is removed but the dummy rectangular field is still visible infront of my camera. Also the mobile platform I work on became lagging after disabling or destroying the canvas.
Later I tried to disable the canvas from GUI as seen in the image below. However result was similar, text is gone but canvas is still there as seen in the screenshot.
For the future, please use the [ code ] and [ /code ] tags (without spaces) to wrap your code into a properly formatted block in your post. Now on to your question.
If I’m not mistaken, this replicates the scenarios you described with comments regarding the effects of it:
// Find the first instance of a Canvas component in the scene
Canvas uiPanel = FindObjectOfType<Canvas>();
// Disable the component, so that it no longer functions
// but the GameObject remains in the scene
uiPanel.enabled = false;
// Find the first instance of a Canvas component in the scene
Canvas uiPanel = FindObjectOfType<Canvas>();
// Destroy the component, but maintain the
// GameObject in the scene
Destroy(uiPanel);
This is will completely destroy the GameObject in the scene:
// Find the first instance of a Canvas component in the scene
Canvas uiPanel = FindObjectOfType<Canvas>();
// Destroy the GameObject that has the referenced
// Canvas component
Destroy(uiPanel.gameObject);
I can’t say what would be causing any lag without a good deal more information and potentially code. Hope the above helps you.