-1

I have a string list of characters e I'm trying to get information from another exclusive .cs file to the main .cs file.

How to change this

  string character_selected;

  if (character_selected == "Jin")
  {
       gameObject.GetComponent<Jin>().getinfo;
  }

  if (character_selected == "Ken")
  {
       gameObject.GetComponent<Ken>().getinfo;
  }

To something like this:

string character_selected;

gameObject.GetComponent<character_selected>().getinfo;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Assuming Jin and Ken are classes (which they have to be if the first code works) then you need to use a different overload of GetComponent https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

      string typeName = $"something.something.{character_selected}";
      var type = Type.GetType(typeName);
       gameObject.GetComponent(type);

the 'something.something' is there because it needs the complete fully qualified class name

pm100
  • 48,078
  • 23
  • 82
  • 145