Skip to content

Can we get component only from specified SerializeField object? #38

@KamilDev

Description

@KamilDev

Can we get component only from specified SerializeField object?

For example:

private class AnimatorOnModelFilter : SceneRefFilter<Animator>
{
    public override bool IncludeSceneRef(Animator animator)
    {
        // Get the EnemyController component
        var enemy = animator.GetComponentInParent<EnemyController>();

        // Check if this animator is on the enemyModel
        return enemy != null && enemy.enemyModel != null && animator.gameObject == enemy.enemyModel.gameObject;
    }
}

[Header("Core Components")]
[SerializeField, Anywhere] protected Transform enemyModel;
[SerializeField, Child(filter: typeof(AnimatorOnModelFilter))] protected Animator _animator;

Essentially I want to do:

_animator = enemyModel.GetComponent<Animator>();

Is there any better or shorter way to achieve what I want?

Edit:
I think I'll just use OnValidate for such cases, as it's less verbose code.

It would be great if there was some way directly as an attribute.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions