Arrow
Arrow

Reputation: 5

DontDestroyOnLoad doesn't work for me in unity

I am working on an application that works as a GPS for a small sector of a public place. In short, on my map I have objects that are the locations (rooms) in this place.

What happens is that I do not want these objects to be destroyed when one changes scene, because otherwise I have to reload them to a list, in which after there, I change their names and colors from a database. But I can't get this to work, surely I'm doing something wrong, but I do not know what.

Image

I have those objects (there are about 300) which, each one, has the same script (the right one)

private void Awake()
{
    {
        {
            DontDestroyOnLoad(this.gameObject);
            AulaDatabase.addAula(this);
        }
    }
}

That is the code, I know that the objects are going to be added every time I enter the scene, I have that in mind, the problem is that they are always being destroyed.

I also tried to make a different script and add it to the parent container of all these objects, but it didn't work either.

It should be noted that with a different object and another script, I used DontDestroy and it worked, but with these objects it is not working, what am I doing wrong?

Upvotes: 0

Views: 757

Answers (1)

shingo
shingo

Reputation: 27254

Because DontDestroyOnLoad only works for root GameObjects or components on root GameObjects, in your screenshot these objects are under the NavigationNames object that make the method not take effect, you can find the document from https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html.

Upvotes: 1

Related Questions