- Home /
Components added programatically - visibility in inspector
Hi, is it possible to add component programmatically, i.e. with gameObject.AddComponent<>()
and make it visible in inspector?
My use case is that I have a base script component, which isn't attached to any prefab - only descendant components are added to related prefabs. In each of those prefabs I need a Sprite Renderer component, and instead of adding it separately for each, I would like to simply add this once, from the level of base Script Component.
Answer by bpaynom · Feb 01, 2019 at 03:28 PM
You can use [RequireComponent(typeof(SpriteRenderer)]
before declaring your component.
[RequireComponent(typeof(SpriteRenderer))]
public class MyComponentClass : MonoBehaviour
{
//Some stuff
}
Your answer
Follow this Question
Related Questions
Inspector vs Script: Component best practice? 1 Answer
Inspector Component Icon - change for derived as well? 0 Answers
Is it possible to create submenus in the "Component Browser" in Editor ? 1 Answer
Can you hide a custom icon for a script in the scene view? 0 Answers
Get specific component on gameobject after order in inspector 1 Answer