Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GameManager Issue

Discussion in 'Scripting' started by gamer123454321, Aug 8, 2020.

  1. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    Hi! I want ask is everyone assign a same GameManager script in a empty object of all scenes?
    I do that because I put Load scene function in GameManager script and I also use Singleton for it, but that cause my a problem. Once a while( maybe 0.3% of times) when other script call GameManager the result is GameManager = null.
     
  2. ibbybn

    ibbybn

    Joined:
    Jan 6, 2017
    Posts:
    193
    If you use a singleton why have it in all scenes?
    I find it best to start all singletons in a preMainMenu scene so you never have a problem with multiple instances.

    Its probably null because it's start or awake method gets called later than your other scripts. You could put it first in the script execution order or just let the game manager persist through all the scenes..
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with Prefab used for predefined data:

    https://pastebin.com/cv1vtS6G

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!
     
  4. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    Thank you
     
    Kurt-Dekker likes this.