NullReferenceException but public static object present

I am periodically taking a value from one script and adding it to a grand total (totalRev) on another script.

The grand total script:

using UnityEngine;
using System.Collections;
public class player1Base : MonoBehaviour
{
	public static player1Base S;
	public int totalRev = 0;

	// Use this for initialization
	void Start ()
	{
	
	}
	
	// Update is called once per frame
	void Update ()
	{

	}
}

The sub-total script:

   	void Deposit()
    	{
    		if(pRev != 0)
    			{
    				nextDeposit = Time.time+(lastDeposit/rOP);
    				print ("deposit" );
    				player1Base.S.totalRev += pRev;
    			}
    	}

The timing works fine, but every five seconds (whenever a transfer is supposed to occur) I get a NullRef as this is not set to an instance of an object. There is only one instance of this script and my (limited) understanding, is that by setting up a Singleton I can access this script from anywhere in my code. I’m re-using this concept from a tutorial I’ve done that works fine, but obviously I’m doing something wrong here. The interesting thing is I’m getting a run-time error not a compile error.
Any help would be appreciated.

In this code, you don’t actually ever set S to anything, so it is null and you get NullRefExc as expected. Just because it is static does not mean it will be automatically created.

Singleton is an OOP design pattern/concept, while static is just a way of referencing some variable without an instance of the class. There are a few ways to create a singleton.

In your case, you can probably just do S = this; in the Start(), assuming you will ever only have one player1Base script in existence.

(Since it seems you only want to store the totalRev, so you could also just make it public static int totalRev = 0;. Then simply access it as player1Base.totalRev += pRev; without needing to have player1Base instance at all.)

P.S. You should name your class Player1Base and name S something like Instance.