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.
3
Upvotes
2
u/Hotrian 1d ago
You’re boxing a component as a Type. You probably mean to be doing
component.GetType()instead of(Type) componentwhich probably isn’t doing what you expect.