I’ve been tearing my hair out for a week now trying to build a dialogue system where all of the lines of a particular interaction are held within an array, and trying to make that array be a value in a Hashtable where the key is just a simple concatenation of two strings that can be grabbed from the scene. But I keep running into trouble when I build the hashtable, where I can assign the arrays as values, but when I use the key, it returns nothing. And workarounds either pass casting issues, slicing issues or “NullReferenceException: Object reference not set to an instance of an object.” I know this is a complicated explanation, and the problem was getting overwhelming, so I built a really simple setup to try and sort out the problem. I have two scripts in this test.
_TestScriptA:
var otherScript: _TestScriptB;
var Box1 : GameObject;
var Box2 : GameObject;
var hash : Hashtable;
function Start () {
Box1 = GameObject.Find("GUI Text 1");
Box2 = GameObject.Find("GUI Text 2");
otherScript = GameObject.Find("Main Camera").GetComponent("_TestScriptB");
hash = otherScript.hash;
}
function Update () {
if(Input.GetKeyDown(KeyCode.Q)){
var x: Array = hash["poop"];
Box1.guiText.text = x[0];
}
}
_TestScriptB:
var poop: Array;
var poop2: Array;
var hash: Hashtable;
function Start () {
poop = new Array("1","2","3","4");
poop2 = new Array("7","8","9","0");
hash = new Hashtable();
hash["poop"] = poop;
hash["poop2"] = poop2;
}
I know it’s gotten a little messy, I’ve tried several iterations to try and resolve the problem. Tried to cut out anything that wasn’t relevant. Hopefully what I’m attempting is clear here: When you push “Q,” the Gui Text of Box1 should become the string “1.”
Any help is appreciated! Using Unityscript, in case that’s not clear.