1

I am working on an RPG in Unity3d. I have successfully refactored the code to run an SQL database and into a singleton pattern. The gameobject is created at runtime.

In each table I create a list based on its property script; I'm creating it there because it accesses the database. The other script is attached to the top panel where I want to display the character's name and stats, but I'm having trouble getting access to the list.

PlayerDatabaseManager PDM; 

void Awake()
{
    PlayerDatabaseManager.Instance.LoadPersonal();
}

// Use this for initialization
void Start () 
{
    PDM.GetComponent<PlayerDatabaseManager>.Personal();
    Personal = PDM.Personal.Name();
}

I'm confused what to put where. Does personal need to be of type databasemanager or of type personal?

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
Ken Gordon
  • 195
  • 6
  • 20
  • if your "singletons" are MonoBehaviour, it will never work, Ken. simply follow the **three simple points** explained here ... http://stackoverflow.com/a/35891919/294884 – Fattie Mar 29 '16 at 19:38

1 Answers1

1

I think your structure is a bit confused. If PlayerDatabaseManager is a singleton, you shouldn't be calling LoadPersonal from a different component - it should be dealing with that itself when it needs to, to be well encapsulated.

Additionally, if PDM is already of type type PlayerDatabaseManager, there is no need to called GetComponent<PlayerDatabaseManager on it.

I think what you want is:

1) In PlayerDatabaseManager Awake():

Instance = this;

2) In other classes that access information, for example the name in Personal

void Start() {
    Name = PlayerDatabaseManager.Instance.Personal.Name()
}

3) Set the execution order for PlayerDatabaseManager so that it runs before other scripts, or make sure it is loaded in the first scene before any other gameobjects with scripts that will access it.

davedavedave
  • 487
  • 6
  • 11