I want to specify that my CircleCollider2D is a trigger
[RequireComponent(typeof(CircleCollider2D), ????? )]
or in this case I want my BoxCollider2D to be used by Effector
[RequireComponent(typeof(BoxCollider2D), ????? )]
I want to specify that my CircleCollider2D is a trigger
[RequireComponent(typeof(CircleCollider2D), ????? )]
or in this case I want my BoxCollider2D to be used by Effector
[RequireComponent(typeof(BoxCollider2D), ????? )]
RequireComponent is just a neat editor tool to automatically add a component to a gameobject when this script is attached through the editor. Note that this won’t actually work when you add your script though code. It’s a pure editor feature. It’s not possible to specify initialization values in an attribute like this.
However what you want to use instead is implement the “Reset” method. Reset is only called once when the component is attached to the object or when you right click the component and select “Reset”. It’s ment exactly for that kind of initialization of your component at edit time.
void Reset()
{
GetComponent<CircleCollider2D>().isTrigger = true;
}
Though it would even be recommended to do the component check directly inside Reset. RequireComponent is not really reliable.
void Reset()
{
var col = GetComponent<CircleCollider2D>();
if (col == null)
col = gameObject.AddComponent<CircleCollider2D>();
col.isTrigger = true;
}
This way you don’t need the RequireComponent at all.
[RequireComponent(typeof(CircleCollider2D))]
public class Foo : MonoBehaviour
{
private CircleCollider2D circleCol2D;
private void Awake()
{
circleCol2D = GetComponent<CircleCollider2D>().isTrigger = true;
}
}