How to access variables inside methods in a class that is stored in an arraylist
I have a bunch of variable definitions stored inside method option1() in class A1L1O1 (stands for act 1 level 1 option 1).
public class A1L1O1 : objectProperties {
public void option1(){
Label2 = "test label text 1";
Text2 = "test label text 1";
Width2 = 20;
Length2 = 20;
Point2 = 1;
}
}
Same thing for A1L1O2.
public class A1L1O2 : objectProperties {
public void option1(){
Label2.Equals("test label text L1O2");
Text2.Equals("test text L1O2");
Width2.Equals(20);
Length2.Equals(20);
Point2.Equals(1);
}
}
objectProperties just initializes the variables, and the child classes define them.
In another class, responseManager, I have an ArrayList Options, and in it I stored A1L1O1() and A1L1O2().
How can I access the variables inside the methods, inside the class? I don't fully understand getComponent but I feel like that's part of the solution. I'm super new to coding, thank you!
Can you also include the code for your objectProperties, and the code you're using to Create your Arraylist, and add your A1L10 classes? Also, how are you calling this option1 method?
public class objectProperties : $$anonymous$$onoBehaviour {
public string Label2;
public string Text2;
public int Width2;
public int Length2;
public int Point2;
public string Label(string Label2){
return Label2;
}
public string Text(string Text2){
return Text2;
}
public int Width(int Width2){
return Width2;
}
public int Length(int Length2){
return Length2;
}
public int Point(int Point2){
return Point2;
}
}
ArrayList Options = new ArrayList();
public int i = 0;
public void Awake(){
Options.Add (new A1L1O1());
Options.Add (new A1L1O2());
Options.Add (new A1L1O3());
}
Answer by thePeine · Jun 20, 2017 at 02:12 AM
OK, gotcha, so there are a few things. First of all, to answer your direct question, you would need to cast each entry to access the underlying fields. So to access Length2 of your first Options item, you'd want
((objectProperties)Options[0]).Length2
However, a few other things I'd like to point out.
Label2.Equals("test label text L1O2"); will not do what you want. You'll have to keep it like you've done in A1L1O1, and do Label2 = "test label text L1O2";
Right now, you're setting values in your option1() function. Problem is, no one ever calls this function. Would probably be best to put these in a constructor instead. So change this:
public class A1L1O1 : objectProperties {
public void option1(){
Label2 = "test label text 1";
Text2 = "test label text 1";
Width2 = 20;
Length2 = 20;
Point2 = 1;
}
}
into this: That way it will be called when you create a new instance, with the "new" keywords
public class A1L1O1 : objectProperties {
public A1L1O1 (){
Label2 = "test label text 1";
Text2 = "test label text 1";
Width2 = 20;
Length2 = 20;
Point2 = 1;
}
}
The lastly, your methods in your base class aren't doing what you want either. You'd want to change this:
public class objectProperties : MonoBehaviour {
public string Label2;
public string Text2;
public int Width2;
public int Length2;
public int Point2;
public string Label(string Label2){
return Label2;
}
public string Text(string Text2){
return Text2;
}
public int Width(int Width2){
return Width2;
}
public int Length(int Length2){
return Length2;
}
public int Point(int Point2){
return Point2;
}
}
Into This:
public class objectProperties : MonoBehaviour {
public string Label2 {get; set;}
public string Text2 {get; set;}
public int Width2 {get; set;}
public int Length2 {get; set;}
public int Point2 {get; set;}
}
I know it's a little confusing to take this all in at once, but feel free to ask if you want clarifications. And realistically, you'll probably want to start with a good C# book / Web tutorial, otherwise taking one off examples will never make sense. Good luck, hope this helped.
Oh, okay. I still don't fully understand getter and setter methods, but I think I see what you did there. I've been using Google, stackoverflow, and Lynda to learn haha. Thank you so much!