Buttons and choosing class constructor
Hello, I have a question about making choices based on button presses. My title might not be accurate and finding the exact words to ask is probably part of my problem finding the answer. So I do apologize for that in advance.
I have the following code which forms a class and has an initiator(?) inside of it I then have two class constructors and finally public methods that print the values of these constructors based on the button that the respective method is attached to.
using UnityEngine;
using System.Collections;
public class testScript : MonoBehaviour {
public class testing{
public int generic_variable;
public testing(int gen_var){
generic_variable = gen_var;
}
}
public testing test_1 = new testing(1);
public testing test_2 = new testing(2);
public void ButtonCheck1(){
Debug.Log(test_1.generic_variable);
}
public void ButtonCheck2(){
Debug.Log(test_2.generic_variable);
}
}
I've tested this out in game and it works perfectly. My question is how can I make this more dynamic and reduce this down to 1 public method that will print the right variable based on which button is pressed.
I know the simple answer is for the buttons to pass some sort of a variable and then to have an if else statement that is conditional based on the variable passed. However, as easy of a solution this is for 2 buttons, I want to be able to eventually expand this to several buttons and I don't want to have this rigid, predetermined length if else statement determining the conditions. I'm thinking it would be something like, the button would pass along the variable, let's say test_x and then the function prints out test_x.generic_variable but I know that coding is not that simple.
Thank you in advance for your help and I would be happy to answer any questions that would help clarify what I am looking to do.
Answer by Alanisaac · Jan 29, 2018 at 11:49 PM
What you're probably looking for is a list of your classes. You can use the index of the class in your list to find the right data for the particular button. In the script below, I named the test class "ButtonData" to hopefully make the connection more clear:
public class TestScript : MonoBehaviour
{
public List<ButtonData> ButtonDatas = new List<ButtonData>();
// Use this for initialization
void Start()
{
ButtonDatas.Add(new ButtonData(1));
ButtonDatas.Add(new ButtonData(3));
ButtonDatas.Add(new ButtonData(7));
ButtonDatas.Add(new ButtonData(0));
ButtonDatas.Add(new ButtonData(5));
}
void ButtonCheck(int index)
{
// because you have 5 items in your list, the index should be a number 0-4
var buttonData = ButtonDatas[index];
// here you can retrieve any variable from the button data
var myButtonNumber = buttonData.MyNumber;
// as an example, for an index of 2, myButtonNumber would be 7
Debug.Log(myButtonNumber);
}
}
public class ButtonData
{
public int MyNumber;
public ButtonData(int myNumber)
{
MyNumber = myNumber;
}
}
Edit: added a few more comments.
I think I'm following what you're saying, but If I remember correctly C# only allows 1 var type for a list. Although this would technically work for this basic proof of concept example, it would break down if you have different variables types for your class correct?
EDIT - It finally clicked. It's not a list of values to be passed to the constructors, its a list OF constructors. Everything else fell into place after that, thank you.