- Home /
Static function and variables error
Hi there :) 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! :)
I don't do JS so don't know for sure, but on 11 in parameter ( ) do you have to tell it what type like string SubsText, int SubsDuration
Thanks for the tip getyour411, but unfortunately this didn't fix the problem. Same error message still appears.
Answer by Loius · Feb 05, 2014 at 01:10 AM
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.
Thank you, removing the SubtitlesControl before GameObject.Find helped :)
Answer by ffxz7ff · Feb 05, 2014 at 12:59 AM
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.
Tried this but unfortunately it didn't help :( Thanks for the tip though.