Singleton in Start Function Error

Hello, :slight_smile:

DBM_clearDatabaseVariables.js


static var instance : DBM_clearDatabaseVariables;

// This is where the magic happens.
// FindObjectOfType(…) returns the first AManager object in the scene.
instance = FindObjectOfType(DBM_clearDatabaseVariables);

if (instance == null)
{
Debug.Log (“Could not locate an DB_Manager object. You have to have exactly one DB_Manager in the scene.”);
}

// Ensure that the instance is destroyed when the game is stopped in the editor.
function OnApplicationQuit()
{
instance = null;
}

function clearDatabaseVariables()
{
var loadingText : GameObject;
loadingText = GameObject.Find(“Loading_Text”);

loadingText.guiText.text = “Clearing…”;

// Global_Variables.mySubscriptions.Clear();
// Global_Variables.curriculaItems.Clear();
// Global_Variables.cameraItems_names.Clear();
// Global_Variables.cameraItems_tx.Clear();
// Global_Variables.cameraItems_ty.Clear();
// Global_Variables.cameraItems_tz.Clear();
// Global_Variables.cameraItems_rx.Clear();
// Global_Variables.cameraItems_ry.Clear();
// Global_Variables.cameraItems_rz.Clear();
// Global_Variables.cameraItems_sx.Clear();
// Global_Variables.cameraItems_sy.Clear();
// Global_Variables.cameraItems_sz.Clear();
// Global_Variables.cameraItems_orthoScale.Clear();

// Global_Variables.mySubscriptions.Clear();

// Global_Variables.curriculaItems.Clear();

// Global_Variables.menuItems.Clear();
}


Startup Object in my scene

function Awake ()
{
Application.targetFrameRate = 15;

new GameObject(“DBM_clearDatabaseVariables”).AddComponent(“DBM_clearDatabaseVariables”);

// new GameObject(“DBM_getActiveSubscription”).AddComponent(“DBM_getActiveSubscription”);
// new GameObject(“DBM_getMySubscriptions”).AddComponent(“DBM_getMySubscriptions”);
// new GameObject(“DBM_getUserCurricula”).AddComponent(“DBM_getUserCurricula”);
// new GameObject(“DBM_getUserCurriculaMenus”).AddComponent(“DBM_getUserCurriculaMenus”);
// new GameObject(“DBM_getCameraPositions”).AddComponent(“DBM_getCameraPositions”);
// new GameObject(“DBM_getUserInfo”).AddComponent(“DBM_getUserInfo”);
}

function Start()
{
var loadingText : GameObject;
loadingText = GameObject.Find(“Loading_Text”);

Time.timeScale = 1;

//CLEAR DATABASE VARIABLES

DBM_clearDatabaseVariables.instance.clearDatabaseVariables();

// //GET THE SELECTED SUBSCRIPTION
// DBM_getActiveSubscription.instance.getActiveSubscription();

// //SET ASSET URL
// Global_Variables.assetsURL = (Global_Variables.dataBank + Global_Variables.activeSubscription + “/”);

// //GET MY SUBSCRIPTIONS
// DBM_getMySubscriptions.instance.getMySubscriptions();

// //GET CURRICULA
// DBM_getUserCurricula.instance.getUserCurricula();

// //GET CURRICULA Menus
// DBM_getUserCurriculaMenus.instance.getUserCurriculaMenus();

// //GET DEFAULT CAMERAS
// DBM_getCameraPositions.instance.getCameraPositions();

// //GET USER INFORMATION
// DBM_getUserInfo.instance.getUserInfo();

loadingText.guiText.text = “Loading Player…”;

//Application.LoadLevel(“MainProgram_1.0”);

Application.LoadLevel(“Test”);
}


How do i “properly” execute a singleton from a function Start without getting a ref obj error?

Any help appreciated.

Thanks,
Oliver :slight_smile:

http://forum.unity3d.com/threads/143875-Using-code-tags-properly

–Eric

You have code outside of functions in your DBM script. That code will never execute.

The terminology “execute a singleton” does not have any meaning. Singletons can’t be ‘executed’, but their functions can be.

class Singleton extends MonoBehaviour {
  static var instance : Singleton;
  function Start() { instance = this; }
  static function X() { }
}
...
Singleton.X();
...

You must either have a Singleton component attached to an object on game startup (by far the simpler solution), or attach it with targetObject.AddComponent(Singleton).