- Home /
Accessing variable from a class
I have a Class which contains multiple variables that are set at Start(). Start() Contains a loop to set those variables multiple times from an array. So.. this is my code currently
for (var x=0; x<gameCount; x++){
var num : int;
var dataArr = new Array();
var d = ""+gameArray[x];
dataArr = d.Split(":"[0]);
// Debug.Log("ToString Data: " + d);
// Debug.Log("dataArr Data; " + dataArr);
var data = new game();
data.id = int.Parse(dataArr[0]);
data.turn = int.Parse(dataArr[1]);
data.user1 = int.Parse(dataArr[2]);
data.user2 = int.Parse(dataArr[3]);
data.base1 = dataArr[4];
data.base2 = dataArr[5];
// Debug.Log(data.user1+" vs "+data.user2);
gameArray[x] = game;
// Debug.Log("From gameList " + data.id);
}
}
public class game extends MonoBehaviour{
var id : int;
var turn : int;
var user1 : int;
var user2 : int;
var base1 : String;
var base2 : String;
}
As you can see, I put in plenty of debugs in there to help me out. Everthing here works. I can even debug the variables from this class and I get the needed values. My biggest problem is that I can not seem to figure out how to properly access these from another script. Am I gonna have to set more then one class for every irritation of that loop? The idea behind this process was so I can do a loop somewhere else to get what I need out of that array. I was attempting to do 'game.gameArray[x].id' and so on. Obviously it was telling me gameArray was not part of class 'game'. So... what is the right way of doing this??
Answer by koo_04 · Mar 26, 2012 at 03:44 PM
I have figured out what I SHOULD be doing. I instead made the variables in the class arrays and accessed them that way. Making them static on top of that made it accessible in another script.
Isn't it just awesome that you go and ask a question, then just after that think of the solution? xD Man programming is fun.
Here is my solution:
for (var x=0; x < gameCount; x++){
var num : int;
var dataArr = new Array();
var d = ""+gameArray[x];
dataArr = d.Split(":"[0]);
var data = new game();
data.id.Push(int.Parse(dataArr[0]));
data.turn.Push(int.Parse(dataArr[1]));
data.user1.Push(int.Parse(dataArr[2]));
data.user2.Push(int.Parse(dataArr[3]));
data.base1.Push(dataArr[4]);
data.base2.Push(dataArr[5]);
gameArray[x] = game;
}
} //this is the end of the function
public class game extends MonoBehaviour {
static var id = new Array();
static var turn = new Array();
static var user1 = new Array();
static var user2 = new Array();
static var base1 = new Array();
static var base2 = new Array();
}