Why destroyed GameObject still shows its GUI elements?

I have a GameObject witch displays a fast login like interface and once you identify other script calls a function on the first to destroy it. This is the code:

string txtLogin = "Login";
    string txtPass = "Pass";
    void OnGUI(){
        txtLogin  = GUI.TextField (new Rect (25, 50, 100, 25), txtLogin);
        txtPass  = GUI.TextField (new Rect (25, 75, 100, 25), txtPass);

        if (GUI.Button (new Rect (10,10,70,30), "Login")) {

            print(txtLogin+" - "+txtPass);
            Business.User user = new Business.User();
            user.Pass=txtPass;
            user.Login=txtLogin;

            Server.loginUser(user);

        }

    }

    public void loginError(){
        txtLogin = "Login ERROR";
    }

    public void loginSuccess(){

         GameObject clientObj = GameObject.Find("Client");
        Client clientComp = clientObj.GetComponent("Client") as Client;
        clientComp.init();

        Debug.Log("BLASBLS");
        Destroy(gameObject);
        Destroy(this);
        Debug.Log("BLASBLS");

    }

But although the gameObject seems to be destroyed, its gui elements still shows on screen. What am I doing wrong???

1 Answer

1

If you destroy only the gameobject, the scripts will keep running. you have to destroy the scripts attached -than- destroy the gameobject.

After the "Destroy(gameObject);" I`m doing "Destroy(this);" to destroy the only script the object has. What other script should I destroy????

You should destroy all scripts related to the object. Scripts that extends MonoBehaviour -can- be attached to an gameobject, however it doesn't mean that they will stop executing if the gameobject is destroyed; and it doesn't mean they need to be attached to execute something.

I still dont get it. I have only 1 script attached to the gameobject. Extending "MonoBehaviour" attaches some script by default? Calling "GUI.<something> attaches a script? If so plz guide me on how to destroy them.