Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Why is RequireComponent Limited to 3 Components?

Discussion in 'Scripting' started by homeros, Jan 22, 2013.

  1. homeros

    homeros

    Joined:
    Dec 23, 2009
    Posts:
    118
    Why is there a limitation on [RequireComponent] and is there anyway to increase this? Obviously using it in one of the required components is a method but in some cases I don't want that.
     
  2. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    I think you should be able to increase that limit by simply using multiple RequireComponent attributes like so:
    Code (csharp):
    1. [RequireComponent(typeof(Foo1))]
    2. [RequireComponent(typeof(Foo2))]
    3. [RequireComponent(typeof(Foo3))]
    4. [RequireComponent(typeof(Foo4))]
    5. class Bar : MonoBehaviour
    6. {
    7.     .
    8.     .
    9.     .
    10. }
    As to why just three arguments in the constructor, I imagine writing multiple constructors that take more arguments gets tedious after a while, and I personally never had to use more than three in my own code. Though even when I do, I place each type in its own RequreComponent attribute on a separate line.
     
    IgorAherne likes this.
  3. homeros

    homeros

    Joined:
    Dec 23, 2009
    Posts:
    118
    I didn't need more than 3 before either. Anyway, your method works fine, thanks.