Destroyed object component are still active?

Im using unity 2018.3.6f1, i got a scene with a back button that disconnect me from the network and get back to login screen which is 2 different scene. Those scene are loaded with scenemanger.loadlevel and i make sure to destroy all objects i have in the dontdestroy in the hierarchy then i load my login scene.

When i connect again, the script are run twice. Then i click back disconnect and connect again, its ran three time. I see it trough my debug.log i have in the script that keep coming back +1 which mean its still running in background. I dont understand why, anyone have an idea?

In the hierarchy i only see 1 instance of the gameobject using the script yet its still ran multiple time…

Hmm could be cause im actually also removing an object which the script destroying the object belong to? I would imagine everything would just stop after it get to “this.gameObject”… But it doesnt.

foreach (GameObject _gameObject in _dontDestroy) {
                        Debug.Log ("Object destroyed: " + _gameObject.name);
                        Destroy (_gameObject);
                    }
                    Destroy (this);
                    SceneManager.LoadScene ("Login");

Objects aren’t destoryed unity the end of the frame, they aren’t instantly removed. So any code in the same method after destroying the object the script is on will still run unless it is waiting until the next frame for some reason such as in a coroutine.

1 Like

I still cant figure out why those script still run after going forth and back from scenes. Ive read on google about delegate (probably my OnEnable OnDisable events for loading scene) which could make the script to stay active… Not sure how to fix. Its a webgl project so ill probably end up just showing a page asking to refresh the page or something. Its ugly tought…

Destroy(this) will destroy the component, not the gameobject it’s on. Since I don’t have a full picture of your code and hierarchy, I don’t know if that’d be the cause of the issue, but it’s worth pointing out.

Otherwise, I don’t have enough context to comment with much confidence.

The gameobject is also in the _gameObject array which is my GameManager object loaded from first scene “Login”, that i then put into dontdestroy hierarchy. In that code up there, i try to destroy the gameobject in the array, then i try to destroy the component, then load the scene login (which have all this again, i just want it anew).

But even by removing the game object and (this) its still sticking in memory somehow. Which affect my login afterware cause the scripts getting the events is the one not destroyed and not the new one i just loaded.

Actually, I take back my earlier statement.

Unsubscribe from whatever delegates you have subscribed to in OnDestroy. C# Components are managed class wrappers around native components. Because of this, the C# object can’t be released from memory until all of its references go out of scope. Your delegate subscription is keeping the C# object in scope, even though the underlying component has been destroyed.

3 Likes

Yea thats what i tought just unsure how to unscribe exactly, dont have much time right now to check the code tought im gonna investigate this tonight. Thanks for your input! :slight_smile:

You unsubscribe from delegates the same way you subscribe.

// subscribe
someDelegate += YourMethod;
//unsubscribe
someDelegate -= YourMethod;