Search Unity

  1. Unity 2018.3 is now released.
    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. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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.