use constructor of class with input value

if the class has a constructor that has 2 input parameters how can getcomponent and call the constructor? after that I assigned the script to a gameobject, the name of gameobject is ‘waiting’. and I want call that class constructor in main script the code like this:

 class class1
         {string s;
          public class1(string ss){s=ss;}

and the main script like this for example:

GameObject mypanel= GameObject.FindWithTag("Waiting");
             class1 mypanel_script= (class1)mypanel.GetComponent<class1>();

how can initialize the value of s when create the mypanel_script?

First of all a class has to be derived from MonoBehaviour and has to be placed in it’s own file where the file name matches the classname in order to be attached as component to a gameobject. Second you can’t create MonoBehaviour derived classes (or components in general) with “new”. Therefore you can’t use a constructor with parameters.

To actually add a component to a GameObject you have to use GameObject. AddComponent< YourClassName >();.

Components can’t live on their own. They always need to be attached to a gameobject.

Finally some additional hints:

  • The generic version of GetComponent returns already the type you specified in the generic parameter, so there’s no need to cast the return type again.
  • Classnames should be PascalCase ( so they should start with a capital letter).