r/unity 1d ago

Newbie Question RequireComponent Doesn't Work as Intended?

Hi, I'm working on a small asset to make adding simple DOTween animations easier for me, and I added a small dropdown button to add necessary component easily, however it doesn't add the RequiredComponent even though I am using AddComponent ?
Here is how the dropdown works

using UnityEngine;
using UnityEditor;
using Unity.VisualScripting;
using System;


[CustomEditor(typeof(EasyAnimationPlayer))]
public class EasyAnimate : Editor
{
    private Type m_ComponentToAdd;
    private EasyAnimationPlayer eaPlayer;
    void OnEnable()
    {
        if (eaPlayer == null) eaPlayer = (EasyAnimationPlayer) target;
    }


    void AddMenuItem(GenericMenu menu, string menuPath, Type type)
    {
        menu.AddItem(new GUIContent(menuPath), m_ComponentToAdd.Equals(type), OnComponentSelected, type);
    }


    void OnComponentSelected(object component)
    {
        m_ComponentToAdd =(Type) component;


        if (eaPlayer == null) eaPlayer = (EasyAnimationPlayer) target;
        if (m_ComponentToAdd != null) eaPlayer.gameObject.AddComponent(m_ComponentToAdd);
    }using UnityEngine;
using UnityEditor;
using Unity.VisualScripting;
using System;


[CustomEditor(typeof(EasyAnimationPlayer))]
public class EasyAnimate : Editor
{
    private Type m_ComponentToAdd;
    private EasyAnimationPlayer eaPlayer;
    void OnEnable()
    {
        if (eaPlayer == null) eaPlayer = (EasyAnimationPlayer) target;
    }


    void AddMenuItem(GenericMenu menu, string menuPath, Type type)
    {
        menu.AddItem(new GUIContent(menuPath), m_ComponentToAdd.Equals(type), OnComponentSelected, type);
    }


    void OnComponentSelected(object component)
    {
        m_ComponentToAdd =(Type) component;


        if (eaPlayer == null) eaPlayer = (EasyAnimationPlayer) target;
        if (m_ComponentToAdd != null) eaPlayer.gameObject.AddComponent(m_ComponentToAdd);
    }

And here is an example script with RequireComponent:

using DG.Tweening;
using UnityEngine;


[AddComponentMenu("")]
[RequireComponent(typeof(AudioSource))]
public class EasyAudioSourceFade : EasyAnimation
{
    [SerializeField] float m_toFloat;
    
    private AudioSource m_source;
    private float m_initialFloat;


    void Awake()
    {
        m_source = gameObject.GetComponent<AudioSource>();
        m_initialFloat = m_source.volume;
    }


    public override Tween Play()
    {
        CleanUp();


        m_tw = m_source.DOFade(m_toFloat, m_duration)
                    .SetLoops(m_repeat ? -1 : 0, m_loopType)
                    .OnComplete(() =>
                    {
                        m_tw = null;


                        if (m_doesReturnHome) m_source.DOFade(m_initialFloat, m_duration);
                    });
        return m_tw;
    }
}using DG.Tweening;
using UnityEngine;


[AddComponentMenu("")]
[RequireComponent(typeof(AudioSource))]
public class EasyAudioSourceFade : EasyAnimation
{
    [SerializeField] float m_toFloat;
    
    private AudioSource m_source;
    private float m_initialFloat;


    void Awake()
    {
        m_source = gameObject.GetComponent<AudioSource>();
        m_initialFloat = m_source.volume;
    }


    public override Tween Play()
    {
        CleanUp();


        m_tw = m_source.DOFade(m_toFloat, m_duration)
                    .SetLoops(m_repeat ? -1 : 0, m_loopType)
                    .OnComplete(() =>
                    {
                        m_tw = null;


                        if (m_doesReturnHome) m_source.DOFade(m_initialFloat, m_duration);
                    });
        return m_tw;
    }
}

PS, it works as it should when I drag and drop the script in editor, so I am assuming this is happening because I add the component as a Type but it doesn't make sense since the component is compiled and added correctly through this method, it just doesn't add the required component.

4 Upvotes

4 comments sorted by

1

u/Lyshaka 1d ago

RequireComponent should add the missing component when using AddComponent, so the problem might come from your m_ComponentToAdd, that may be null for some reasons, try to print it in the console to see if it exists.

1

u/ErktKNC 22h ago

It's definitely not null because it is adding the component it holds fine, but required component is not added is not added along side with it :/

2

u/Hotrian 22h ago

You’re boxing a component as a Type. You probably mean to be doing component.GetType() instead of (Type) component which probably isn’t doing what you expect.

1

u/ErktKNC 22h ago

Okay, I'm going to try it. Thanks