Trying to do something like ability.GetComponent<C#script>();

Hello people!

Have a little trouble trying to access an arbitrary script as a component for my ability script in a game I’m creating.

Since i have many characters in the game, I want each of them to have their own ability script, but i need to link them to each other.

So, is there a way to do something like the title:

ability.GetComponent<UnityScript>();

Thanks in advance

A script can only be a component of an object. You can put another script on the same object and call it.

I would suggest you have a scriptableObject attached to your class to give your chars the desired different abilities by attaching the correct scriptableobject.

Yes, but you don’t refer to it by the language, you refer to it by the name of the class.

ability.GetComponent<ClassNameGoesHere>();

Previous answers should be enough but just to cover the basics, if it’s just a class, you could just make a new instance, it’s not recommended if all your skills are the same for all players. But if they have different settings then it’s ok.

MyScriptClass myskill = new MyScriptClass();
//and then update the settings of myskill

You want to use interfaces.

ArbitraryScript.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArbitraryScript : MonoBehaviour {

public int RandomNumber;

    void Awake() {

        RandomNumber = 4;

    }

}

AbilityScript.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AbilityScript : MonoBehaviour {

    // reference the ArbitraryScript (attach it to an empty GO)
    public GameObject ArbitraryScriptGO;

    // reference the Arbitrary Script component of the GO
    private ArbitraryScript arbitraryScript;

    public int SecondNumber;

    void Awake() {

    // find the Arbitrary Script GO (or drag/drop it in the inspector window and remove this line)
        ArbitraryScriptGO = GameObject.FindWithTag("ArbitraryScriptGO");

        // reference the ArbitraryScript.cs script component of the GameObject
        arbitraryScript = ArbitraryScriptGO.GetComponent<ArbitraryScript> ();

    }

    void Start() {

        // access the RandomNumber int from the ArbitraryScript
        //and use it to assign an integer to the SecondNumber variable in this script
        SecondNumber = ArbitraryScript.RandomNumber;

    }

}

If you aren’t comfortable with scriptable objects or interfaces, you can also consider a central ability script that itself calls your individually tailored ability scripts. It will be more cumbersome than the alternatives, but you may be more comfortable doing it this way.

So in the below you’d throw on the Ability script on all your characters, and then you’d also add one of the specific ability scripts to the character which would handle how each individual ability operates. I’m not saying this is the best way, it just is an option you might like.

public class Ability : Monobehaviour {

    private MagicAbility magicAbility;
    private ThiefAbility thiefAbility;
    private KnightAbility knightAbility;

    public enum AbilityType { None, Magic, Thief, Knight };
    private AbilityType equippedAbility = AbilityType.None;

    void Start () {
        magicAbility = GetComponent<MagicAbility>();
        thiefAbility = GetComponent<ThiefAbility>();
        knightAbility = GetComponent<KnightAbility>();
        if (magicAbility != null)
        {
            equippedAbility = AbilityType.Magic;
        }
        if (thiefAbility != null)
        {
            equippedAbility = AbilityType.Thief;
        }
        if (knightAbility != null)
        {
            equippedAbility = AbilityType.Knight;
        }

        if (equippedAbility == AbilityType.None)
        {
            Debug.Log("Warning, there is no specific ability script attached to this object");
        }

    }

    //Call this to use whatever ability this character is able to use, no matter what the ability is
    public void UseAbility()
    {
        switch (equippedAbility)
        {
            case AbilityType.Magic:
                magicAbility.UseSpecificAbility();
                break;
            case AbilityType.Thief:
                thiefAbility.UseSpecificAbility();
                break;
            case AbilityType.Knight:
                knightAbility.UseSpecificAbility();
                break;
            default:
                Debug.Log("Hit default in UseAbility");
                break;
        }
    }

}
public class MagicAbility : Monobehaviour {

    public void UseSpecificAbility()
    {
        //Do your magic stuff here
    }
}
public class ThiefAbility : Monobehaviour {

    public void UseSpecificAbility()
    {
        //Do your thief stuff here
    }
}
public class KnightAbility : Monobehaviour {

    public void UseSpecificAbility()
    {
        //Do your knight stuff here
    }
}