null object reference

I’m trying to access a variable in other script but it only gives me:

NullReferenceException: Object reference not set to an instance of an object

I’m trying to access ‘sanity’ variable from my slenderAI script.

I’m trying to access them with this code:

using UnityEngine;
using System.Collections;

public class StaticScreen : MonoBehaviour {
	
public slenderAI script;
	
void Start(){

	script = GetComponent<slenderAI>();
}
	
void Update(){
		
		if(script.sanity < 100){
			
		Debug.Log("Hi");
			
		}
		
	
	}
}

You mentioned in the above comment that slenderAI is not on the same object. If that’s the case, script is being set to null inside Start(), since GetComponent() will return null.

If you setting the script variable in the inspector, then you just need to remove that line in Start(), I believe. If you are not setting script in the inspector, then you need some other way to get a reference to it.