- Home /
Problem reading from array at runtime
Ok, so basically I am making a game engine, and am having it so it reads from a code file at runtime. This is a short snippet that is causing the problem: (varList is a string, varArray is an array, I am splitting spaces to read the code file, so str is the string after the first space, the cases is the word before the first space)
case "var":
varList = varList + str + "|";
varArray = varList.ToString().Split("|"[0]);
case "getkey":
var varIndex : int = 999;
var error : boolean = false;
var isDone : boolean = false;
for (var iCount : int = 0; iCount < varArray.Length; iCount++) {
if (varArray[iCount] == str && isSub && error == false && isDone == false) {
varIndex = iCount;
error = false;
isDone = true;
}
}
if (error || isDone == false || varIndex == 999) {
print("Variable, '" + str + "' not found!");
} else {
var key;
while (key == "") {
key = Input.anyKeyDown;
}
print("Key pressed : " + key.ToString());
}
break;
sorry, formatting a bit messed up, but basically its throwing this error repetitively: NullReferenceExceptioon: Object reference not set to instance of an object Boo.Lang.Runtime>RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs System.Object rhs)
Referring to the line of code: varList = varList + str + "|";
Print out both "str" and "varList" to find which one is null.
Thanks for the reply, this is my new 'var case' and it doesnt get up to the printing out of str and varList
case "var":
varList = varList + str + "|";
varArray = varList.ToString().Split("|"[0]);
print(str);
print(varList);
Put it before the line with the error.
We need to see the whole script to help more man....
Answer by robhuhn · Jan 02, 2014 at 08:53 AM
You should declare varList with an initial value (empty string). Instead of something like private string varList; write
private string varList = "";
Your answer
Follow this Question
Related Questions
Finding What Material Is On The Object Player Collided With. 1 Answer
Can I get the a list of the properties in a shader at runtime? 1 Answer
load images into an array runtime. 0 Answers
Initiate class array at start and assign class values. 0 Answers
what is the best way to implement minigames into my own full blown unity game. 0 Answers