Storing script names in a variable

Hi, new to unity so please ignore if I sound stupid.

I have to scripts references in a script. Example, script A and B are referenced in script C.

I need to store script A and B variable names in a another variable so that I can use that variable in conditioning.

private FemalePlayerAnimations femalePlayerAnimations;
private MalePlayerAnimations malePlayerAnimations;

private Variable variable; // Got Problem Here

void Awake()
{
    femalePlayerAnimations = GetComponent<FemalePlayerAnimations>();
    malePlayerAnimations = GetComponent<MalePlayerAnimations>();
}

void Start()
{
    if(1 + 1 = 2) // Some Condition
     {
        variable = femalePlayerAnimations;
     }
    else if(1 + 2 = 3) // Some Another Condition
     {
        variable = malePlayerAnimation;
     }
}

Thanks in advance.

This is a really good question for you to have asked because I think there are a few interesting takeaways from this answer!

Firstly, since you have two different classes that will both provide you with a common service (Female/MalePlayerAnimations, presumably will both provide you with a set of clips or something similar) this is a great place to use either an interface or superclass.

public interface class I_AnimationProvider
{
    void GetAnimations(); // or whatever you do in both male + female animations scripts
}

public class FemalePlayerAnimations : I_AnimationProvider
{
    public List<AnimationClip> GetAnimations()
    {
        return femaleAnimations;
    }
}

public class MalePlayerAnimations : I_AnimationProvider
{ 
    public List<AnimationClip> GetAnimations()
    {
        return maleAnimations;
    }
}

The above shows how both animation scripts can implement an interface. This means you know any object that implements I_AnimationProvider will have a function called GetAnimations.

public enum AnimationSet
{
    Male,
    Female,
    // Animal, etc..
}

[SerializeField] private AnimationSet animationSet;

private FemalePlayerAnimations femalePlayerAnimations;
private MalePlayerAnimations malePlayerAnimations;
private I_AnimationProvider ourAnimationProvider;

private void Awake()
{
    femalePlayerAnimations = GetComponent<FemalePlayerAnimations>();
    malePlayerAnimations = GetComponent<MalePlayerAnimations>();
}

private void Start()
{
    if (animationSet == AnimationSet.Female) // Some Condition
    {
        ourAnimationProvider = femalePlayerAnimations;
    }
    else if (animationSet == AnimationSet.Male) // Some Another Condition
    {
        ourAnimationProvider = malePlayerAnimation;
    }
}

Now you can ignore the male / female animation scripts that you found and use only the animation provider to access your animation data. I’ve added an enum to this script as this will dictate which type of animation you use, think of this as a nicer way of having a bool saying ‘usingMaleAnimations’. It’s nicer because if you decide you want more animation types then you can add a new element to the enum and a new else-if statement to the start function checking for that new enum element.

You can do a pretty similar thing by swapping out the interface for an abstract class, this would mean both Male/FemalePlayerAnimations inherit from the same type. You can then have a single variable just called PlayerAnimations and it will find either the male or female version - just make sure the gameobject only has the desired one added to it only!

This is how I’d modify your current code to work as you expect and if you’re happy with this feel free to stop reading here. However, I think there is a much simpler way to do this: (don’t mix up code below with code above, they’re two different ways to do this!)

ScriptableObjects are a way of making persistent data assets. Instead of making them in the scene you will make them in the project and hold various settings / configs / data. I’m thinking that you could have a scriptable object class that stores a list of animations, then you can do away with both male/female player animation scripts and instead of containing a reference to either you have a reference to your scriptable object which holds the animations.

[CreateAssetMenu(menuName = "Create PlayerAnimationData")]
public class PlayerAnimationData : ScriptableObject
{
    // whatever data your male/female classes used to hold, animation clips maybe?

    public AnimationClip[] Animations;
}

You can use this class, then in Assets → Create menu you should see "Create PlayerAnimationData appear! You can make two, one for male and one for female then instead of having to worry about checking about what setting you’ve set, you simply use one of these objects.

[SerializeField] private PlayerAnimationData animationData;

private void Awake()
{
    // not needed!
    //femalePlayerAnimations = GetComponent<FemalePlayerAnimations>();
    //malePlayerAnimations = GetComponent<MalePlayerAnimations>();
}

private void Start()
{
    // not needed! - use your referenced animationData instead
    //if (animationSet == AnimationSet.Female) // Some Condition
    //{
    //    ourAnimationProvider = femalePlayerAnimations;
    //}
    //else if (animationSet == AnimationSet.Male) // Some Another Condition
    //{
    //    ourAnimationProvider = malePlayerAnimation;
    //}
}