Object reference not set to an instance of an object.

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using SharpConnect;

public class LinkSyncSCR : MonoBehaviour {
	public Connector test=new Connector();
	void Start () {Debug.Log(test.fnConnectResult());}
	void Update () {}
	
	void OnApplicationQuit (){
		test.fnDisconnect();
	}
}

Dies on the OnApplicationQuit
test.fnDisconnect() need to be called when the application is shutting down, where should I place that code since OnApplicationQuit doensn’t know about test?

Edit:
The OnApplicationQuit thinks test is de-initialized.
Thats the problem
When I have a successful connection open, test is live and it appears to work correctly, but if my connection failed for any reason, test should not be de-initalized although the Disconnect should return from my other code a “Not Connected” message.

Anywho, thats the problem.
I still need somewhere else to place that code.

The solution is to put

if (test) test.fnDisconnect();

You get that error, it almost always means that you’ve called a .method on a variable that’s null.

Thats the kicker, test should not be null.
Whether or not the user connects, test object in this case should not be null.
I’ll give your suggestion a try, thanks :slight_smile: