Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is there a way to Require duplicate components?

Discussion in 'Scripting' started by AngryBear+^, Aug 1, 2014.

  1. AngryBear+^

    AngryBear+^

    Joined:
    Apr 4, 2009
    Posts:
    68
    Hi all,
    I'm currently trying to write a custom character controller for 2D (I know there are some out there but I feel a need to reinvent the wheel somewhat). To do this I'm making a 2D capsule, but I would like the custom script I'm writing to require that 1 box collider and 2 circle colliders are on the object.

    Using [RequireComponent(typeof(CircleCollider2D)] twice does not create a second circle collider, and I'm unsure of how to make this behavior forcibly add a second circle collider to the object.

    Any insight would be much appreciated.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    If you are only using RequireComponent to add the ohters during edit mode (ie, you add the controller, and it auto-adds the colliders) then you can use void Reset() to addComponent, it calls once when the component is added to a gameobject in the editor.
     
  3. hammil

    hammil

    Joined:
    Jun 5, 2013
    Posts:
    56
    Unfortunately, you can't have multiple components of the same type on one GameObject. In addition, you can't have multiple colliders either. You'll need to either use GetComponentInChildren on awake, or have a reference in your script.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Sure you can.... to both points
     
  5. hammil

    hammil

    Joined:
    Jun 5, 2013
    Posts:
    56
    .. You're quite right! When I tried, I had a 3D rigidbody, which caused the confliction error. My mistake!