Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What are the proper ways or best practices to change scenes and quit the application in Unity?

Discussion in 'Editor & General Support' started by Towsif-Khan, Apr 21, 2019.

  1. Towsif-Khan

    Towsif-Khan

    Joined:
    Jan 9, 2016
    Posts:
    4
    I am aware of the lines of codes used to both quit the game and switch scenes. But in doing so Unity is ravage with a butt load of errors.

    Some of them include
    *missing references
    *Objects spawned on Destroyed

    I am unable to understand some of these bugs let alone fix them. I was hoping to learn the best practices when it comes to these processes so as to both fix these issues and avoid any new ones in the future. Google-ing so far has been futile.
     
  2. Creditc0de

    Creditc0de

    Joined:
    Jan 20, 2019
    Posts:
    4
    Scene change:
    Code (CSharp):
    1. Application.LoadLevel("scenename");
    Quit:
    Code (CSharp):
    1. Application.Quit();
     
  3. Towsif-Khan

    Towsif-Khan

    Joined:
    Jan 9, 2016
    Posts:
    4
    As I have mentioned above. I am well aware of these codes. But I get errors on everything else when I do use them.

    Namely,
    *Lost references
    *Objects spawned from "OnDestroy"
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    Well, those are the correct ways to do it. As for your errors, list them specifically.

    The issue with objects being spawned in OnDestroy could be the result of various sloppy code. One culprit would be classes that use a singleton pattern and instantiate a new instance if the instance is null. In such cases, the singleton might be destroyed, but then some other object might try to access it, causing it to instantiate a new one while the scene is being destroyed. But it could be for other reasons. You'll need to show the code that's causing the new objects to be created.

    Anyway, in short, those errors aren't because you're loading/quitting incorrectly. It means your other code is incorrect.
     
    Joe-Censored and xVergilx like this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    There is no substitute for investigating these on your own. You need to figure out what specific objects are being spawned, and why they are being spawned, and also what specific references are missing.