Static function and variables error

Hi there :slight_smile: I’m making a script for my game that would allow me to control subtitles (text, duration) from another scripts. A private script worked fine, but when I tried to convert it to a static one, I’ve come to a set of problems. I managed to fix many of them, but there’s one I can’t seem to fix. This is my SubtitlesControl.js script:

static var Subtitles : GameObject;
static var Subs : GUIText;
static var SubsOccupied : boolean;

function Start(){
	SubtitlesControl.Subtitles = SubtitlesControl.GameObject.Find("SubtitlesObj");
	SubtitlesControl.Subs = SubtitlesControl.Subtitles.GetComponent("GUIText");
	SubtitlesControl.SubsOccupied = false;
}

static function SubsSay(SubsText,SubsDuration){
	SubtitlesControl.SubsOccupied = true;
	SubtitlesControl.Subs.text = SubtitlesControl.SubsText;
	yield WaitForSeconds(SubsDuration);
	SubtitlesControl.Subs.text = "";
	SubtitlesControl.SubsOccupied = false;
}

However, whenever I try to call “SubtitlesControl.SubsSay(“Blah blah blah”,2);” from another script, it spams the debug with error messages: “NullReferenceException: Object reference not set to an instance of an object”, evidently in 13th row of the script. Can someone help me with that please? Thank you! :slight_smile:

Ummm try changing

SubtitlesControl.Subs.text = SubtitlesControl.SubsText;

to

SubtitlesControl.Subs.text = SubsText;

I can’t see where you defined SubtitlesControl.SubsText but that’s not what you’re passing.

This line should throw a compile-time error:

SubtitlesControl.Subtitles = SubtitlesControl.GameObject.Find("SubtitlesObj");

Regardless, you want GameObject.Find, not SC.GO.Find

Your null reference is specifically because the “.subs” object was not set.

And as fsxffhsfsfsfsf says, you also want to look at your usage of SubsText.