- Home /
Can I choose what to pass into .getcomponent<{VARIABLE}>();
I have this code:
 //do toggle code
 GameObject enabledOption = Instantiate(boolInputPrefab, componentOptionsParent.transform);
 enabledOption.transform.SetParent(componentOptionsParent.transform);
 enabledOption.transform.GetChild(0).GetChild(0).GetComponent<TMP_Text>().text = "Toggle Thruster";
     
 Toggle toggle = enabledOption.transform.GetChild(0).GetComponent<Toggle>();
     
 toggle.isOn = blockRefrence.transform.GetChild(0).GetComponent<Thruster>().thrusterOn;
 toggle.onValueChanged.AddListener(delegate { onToggleComponentValueChange(toggle, blockRefrence, blockType.blockType); });
     
 //do keybind code
 GameObject keybindOption = Instantiate(textInputPrefab, componentOptionsParent.transform);
 keybindOption.transform.SetParent(componentOptionsParent.transform);
 keybindOption.transform.GetChild(0).GetChild(0).GetComponent<TMP_Text>().text = "keybind (1 lowercase letter)";
     
 TMP_InputField textbox = keybindOption.transform.GetChild(0).GetComponent<TMP_InputField>();
     
 textbox.text = blockRefrence.transform.GetChild(0).GetComponent<Thruster>().keybind;
 textbox.onValueChanged.AddListener(delegate { onTextComponentValueChange(textbox, blockRefrence, blockType.blockType); });
See how the toggle code and keybind code look sorta the same? they both look the same except for some things like Toggle toggle = ... and TMP_InputField textbox = ... is there a way I could make a function for that can make variables? like:
 enum VariableType
 {
 //they match the class names
 Toggle,
 TMP_InputFeild
 }
 public classTypeOrSmthn CreateVariable(VariableType kindOfVariableToMake)
 {
     return new kindOfVariableToMake;
 }
 CreateVariable(VariableType.Toggle) newToggleVariable = ...
is this even possible in some way or another?
Answer by Hellium · Jun 03, 2021 at 02:06 PM
You could do something like this.
/!\ CODE NOT TESTED
 private void CreateVariable<T>(GameObject original, Transform parent, string text, Transform blockReference) where T : Component
 {
     //do toggle code
     GameObject clone = Instantiate(original, parent);
     clone.transform.GetChild(0).GetChild(0).GetComponent<TMP_Text>().text = text;
     T component = clone.transform.GetChild(0).GetComponent<T>();
     if (component is Toggle toggle)
     {
         toggle.isOn = blockReference.GetChild(0).GetComponent<Thruster>().thrusterOn;
         toggle.onValueChanged.AddListener(delegate { onToggleComponentValueChange(toggle, blockReference, blockType.blockType); });
     }
     else if (component is TMP_InputField input)
     {
         input.text = blockReference.transform.GetChild(0).GetComponent<Thruster>().keybind;
         input.onValueChanged.AddListener(delegate { onTextComponentValueChange(input, blockReference, blockType.blockType); });
     }
 }
And call it like this:
  CreateVariable<Toggle>(boolInputPrefab, componentOptionsParent.transform, "Toggle Thruster", blockRefrence);
  CreateVariable<TMP_InputField >(textInputPrefab, componentOptionsParent.transform, "keybind (1 lowercase letter)", blockRefrence);
Your answer
 
 
             Follow this Question
Related Questions
object assigns but does not show when added to list, or any other var 0 Answers
Access to a variable inside a C# class 2 Answers
Accessing a variable inside a class inside other script... 2 Answers
How to properly change variables of a sub class 0 Answers
Need my function to work with different lists of different values (classes) 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                