Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. 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.