So basically in my game there are Ally characters that follow the player and perform specific actions.
To keep things simple, there are two Scripts per Ally character, one handling movement the other handling the attack or action function. This script is just called “special script” for sanity’s sake.
allyChar.GetComponent ().enabled = true;
So each character will have a unique Special Script (labelled A, B, C, etc) which does something different. What I want to know is how I can use a single Get Component or similar command to call Special Script regardless of if it is A, B or C.
The allyChar is the Tag for the multiple Ally characters, and I have the FindWithTag working so that’s not an issue.
This feels like something really simple so I apologies if it is.
if each special script acts the same, as in what inputs and outputs it has, why not make a parent class that has all of those, then extend the special scripts from those instead of MonoBehaviour. then you should be able to use allyChar.GetComponent<SpecialClass>();
to get the special class you have attached no matter what it does.
example:
public class SpecialClass : MonoBehaviour { // empty functions for the other classes. } public class SpecialA: SpecialClass { // those same functions as above, but filled out how they are now. }
If i understand every character will have multiple scripts, which you want to disable / enable right?
If so, make another script for doing that action. Name it for example “ActionScript”.
Inside that script make a method with input arguments:
public void SwitchSpecial (int specialIndex)
{
switch (specialIndex)
{
case 0:
GetComponent<ScriptA>().enabled = true;
break;
case 1:
GetComponent<ScriptB>().enabled = true;
break;
// etc...
}
}
then you can calll that “ActionScript” via another script that will call the actions…
p.s. - Code is not checked. I have wrote it here without checking if it works in Unity…
is this what you are looking for??