RequireAnyComponent

  06. Attributes No Comments

When added before a component class will make sure that whenever the component with the attribute is added to GameObjects, at least one component matching one of the specified types is also present on the same GameObject.

Attribute Target

Component class

Example

using System;
using UnityEngine;
using UnityEngine.UI;
using Sisus.Attributes;

[RequireAnyComponent(typeof(Renderer), typeof(Graphic)), DisallowMultipleComponent]
public class Invisible : MonoBehaviour
{
	void Awake()
	{
		var renderer = GetComponent<Renderer>();
		if(renderer != null)
		{
			renderer.enabled = false;
		}

		var graphic = GetComponent<Graphic>();
		if(graphic != null)
		{
			graphic.enabled = false;
		}

		if(renderer == null && graphic == null)
		{
			throw new InvalidOperationException("No Renderer or Graphic components found on GameObject with the Invisible component.");
		}
	}
}

Example Result (Add Component)

 

Example Result (Remove Component)