object reference required to to access non-static member, userSession

I’m trying to get userSession, a variable to put in to my highscore database but i keep getting object reference error.
My SessionScript get the user’s name that is logged in from the User table database and display it.

public class SessionScript : MonoBehaviour {

	public Text userName;
	public static string userSession;

	void Start()
	{		
		userSession = "";
		userName.text = "";
		
		if (!DBContoller.newAcc) 
			userSession = MainMenu.userName;
		else 
			userSession = CreateAcc.username;
		
		userName.text = "Hi, " + userSession;
		Debug.Log ("New Session: " + userSession + "

New Account: " + DBContoller.newAcc);
}
}

Then i want to access the userSession variable and when the user finish playing the game, it will insert the user’s highscore and stuff. Heres where is insert the score.

if(card.cardPosition == 42)
					{
						TimerScript.SetTimeStop();
						Debug.Log("Wrong Key Press = " + wrongKeyPress);
						Debug.Log("Time Passed = " + Mathf.RoundToInt(TimerScript.seconds));
						wrongKeyPress = FinalWrongKeyPress;
						finalSeconds = Mathf.RoundToInt(TimerScript.seconds);
						if( SessionScript.userSession == null)
						{
							return;
						}
						if ( SessionScript.userSession!= null)
						{
							HighScoreManager.InsertScores(SessionScript.userSession,finalSeconds,wrongKeyPress);
						}

						GameEnd();
					}

But i keep getting the error : An object reference is required to access non-static member `HighScoreManager.InsertScores(string, int, int)’

Thanks for helping me in advance!

EDIT** I realized that my InsertScore is on another scene. Therefore i can’t even reference an object as InsertScore is on another scene that is on another script.

Hi @roszetm,

I believe that you are mixing things a bit, that’s why you are getting into architectural problems. The correct approach to this kind of management (databases, cloud services and the like) is to design the code using the Singleton Design Pattern.

To implement this in Unity is quite easy. First off, you need the proper structure and you must make it “invulnerable” to the scene loading/unloading. Moreover, you don’t want to load it if you don’t need it, so you have to implement a lazy instancing approach. Here’s a good generic skeleton that you can use for any similar implementation:

    using UnityEngine;
    using System.Collections;
    
    public class SingletonSkeleton : MonoBehaviour {
    
    private static SingletonSkeleton me;
    private static SingletonSkeleton Me 
    {
        get
        {
            if (me != null) return me;
            GameObject tmp = new GameObject("My Singleton Manager");
            me = tmp.AddComponent<SingletonSkeleton>();
            DontDestroyOnLoad(tmp);
            return me;
        }
    }
		
    private int someScoreVariable;

    public int SomeScoreVariable
    {
        get
        {
            return Me.someScoreVariable
        }
        set
        {
            Me.someScoreVariable = value;
        }
    }

    public static void SomeStaticHelper()
    {
        Me.SomeInstanceMethodHere();
    }
    void Start()
    {
        // Your start code here
    }

    void SomeInstanceMethodHere()
    {
        // Some code here
    }
}

As you can see this will create a game object to contain its own instance if it does not exists, and will make it persistent across scenes. Basically you will only call the static helpers to get access to the instance and its services, and you can do that from any scene in your game. you can also add static properties the same way as shown in the code,

Hope this helps :wink:

Pino