Search Unity

Array of MonoBehaviour

Discussion in 'Scripting' started by Givago, Oct 25, 2017.

  1. Givago

    Givago

    Joined:
    Oct 22, 2015
    Posts:
    27
    Hi,
    I'm having a problem, I'm trying to make an array of multiple scripts. Then I found the suggestion of:

    Code (CSharp):
    1. public MonoBehaviour [] something
    however I can not add prefabs, so I've solved add the scripts in this obj and just deactivating. However now and I can not add the scripts in it as in the example:


    Code (CSharp):
    1. int randomPower = Random.Range (0, something.Count);
    2. other.gameObject.AddComponent <something[randomPower]> ();
    Someone please help me, I've been looking for hours, but none has solved the problem.
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    That won't work, you need the type as a parameter:
    Code (CSharp):
    1. other.gameObject.AddComponent(something[randomPower].GetType());
    [/QUOTE]

    Maybe somebody can help with what you want to achieve but this is something I never saw before.
    What you could do is using a set of strings:
    Code (CSharp):
    1.  
    2.     public string[] scripts;
    3.     // Use this for initialization
    4.     void Start()
    5.     {
    6.         int randomPower = UnityEngine.Random.Range(0, scripts.Length);
    7.         var type = Type.GetType(scripts[randomPower]);
    8.         gameObject.AddComponent(type);
    9.     }
    You have to include namespaces in GetType() if there are any.
    Another way is using hard coded Types:

    Code (CSharp):
    1.  
    2.     public Type[] scripts = new Type[]    {
    3.     typeof( Script1),
    4.     typeof( Script2),
    5.     typeof( Script3)
    6.     };
    7.     // Use this for initialization
    8.     void Start()
    9.     {
    10.         int randomPower = UnityEngine.Random.Range(0, scripts.Length);
    11.         gameObject.AddComponent(scripts[randomPower]);
    12.     }
    or so:

    Code (CSharp):
    1.  
    2.     public string[] scripts;
    3.     // Use this for initialization
    4.     void Start()
    5.     {
    6.         int randomPower = UnityEngine.Random.Range(0, scripts.Length);
    7.         gameObject.AddComponent(GetPower(scripts[randomPower]));
    8.     }
    9.  
    10.     Type GetPower(string power)
    11.     {
    12.         switch (power)
    13.         {
    14.             case "Power1": return typeof(Power1);
    15.             case "Power2": return typeof(Power2);
    16.             default:
    17.                 break;
    18.         }
    19.         return null;
    20.     }
    That is all that comes into my mind.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't think you need to work as hard as all that. This is the one case where the version of AddComponent that takes a type name as a string would be preferable to the strongly-typed version.

    ...Oops, but I see Unity has deprecated that. Bad Unity! No biscuit!

    So either you use the deprecated function while you still can, or you monkey around with Types as @McDev02 has shown.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Or use the string to evaluate a Type. We actually do that fairly often.