Not sure how to make my classes interact in the way I intend them to
Alright, I'm kind of a newbie at C# and Unity so forgive my ignorance and messy code.
I have three classes. First, buttonManager.
public class buttonManager : MonoBehaviour {
public Button First;
public Text Second;
public void Disappear(){
First.gameObject.SetActive (false);
}
public void Appear(){
First.gameObject.SetActive (true);
}
public void Disable(){
First.interactable=false;
}
public void Enable(){
First.interactable=true;
}
public void changeText(){
}
public void changeWidth(){
}
public void changeLength(){
}
public void addPoints(){
}
}
Then, objectProperties.
public class objectProperties : MonoBehaviour {
public string Label2;
public string Label1{
get {
return Label2;
}
set{
Label2 = value;
}
}
public string Text2;
public string Text1{
get{
return Text1;
}
set{
Text2 = value;
}
}
public int Width2;
public int Width1{
get{
return Width2;
}
set{
Width2 = value;
}
}
public int Length2;
public int Length1{
get{
return Length2;
}
set{
Length2 = value;
}
}
public int Point2;
public int Point1{
get{
return Point1;
}
set{
Point2 = value;
}
}
}
And finally, responseManager
public class responseManager : MonoBehaviour {
ArrayList Options = new ArrayList();
ArrayList Responses = new ArrayList ();
public int i = 0;
public void Awake(){
Options.Add (new A1L1O1());
Options.Add (new A1L1O2());
Options.Add (new A1L1O3());
Responses.Add (new A1L1R1 ());
Responses.Add (new A1L1R2 ());
Responses.Add (new A1L1R3 ());
}
public void Start(){
objectProperties o = new objectProperties ();
for(int x = 0; x>=i; x++) {
((objectProperties)Options[i]).Label2 = o.Label1;
((objectProperties)Options[i]).Text2 = o.Text1;
((objectProperties)Options[i]).Width2 = o.Width1;
((objectProperties)Options[i]).Length2 = o.Length1;
((objectProperties)Options[i]).Point2 = o.Point1;
}
}
public void increaseI(){
i++;
}
}
A1L1O1() and all the other classes follow the same format as follows.
public class A1L1O1 : objectProperties {
public A1L1O1(){
Label2 = "test label text 1";
Text2 = "test text 1";
Width2 = 20;
Length2 = 20;
Point2 = 1;
}
}
I need to get the values from responseManager into buttonManager, so that changeText() changes the text of the target object (a UI button in this case) to whatever the text is in A1L1O1 and its sibling classes. From what I've read, I think getComponent is a useful tool I need to learn how to use but I don't really understand how to use it. Am I approaching this correctly? How can I get it to work? Thank you!
Answer by UnityCoach · Jul 05, 2017 at 05:55 AM
MonoBehaviours shouldn't implement constructors. You instead initialise them within their Awake () call.
It seems to me that you're using new inheriting classes for what a simple instance would be more appropriate.
If A1L1O1 is of type objectProperties, why not just create a new objectProperties object and set the properties in the editor?
I'm going to have dozens of these A1L1O1-like objects, probably around 50. I want to create one button, and change the text of the button based on the information stored in each A1L1O class, ins$$anonymous$$d of creating individual objects for all of them.