How to get one of many scripts in fastest way possible

In my game, I have a character walk up to a vehicle, and upon entering a trigger, it finds and stores the driving script for that one vehicle, say “drive1,” in a public drive1 script variable so that the variable can be assigned with GetComponent() so that it can use data from it, but if I have multiple vehicles with script drive2, drive3, so on, is there a faster and more efficient way than having a drive1 and drive2 variable then testing if the vehicle has drive one, store that, else if it has drive2, store that, and so on?

Why do you have different script for vehicles? Use one common control script for all vehicles and store it in one variable. But if you really want different control scripts for different vehicles, you may implement common interface and strore one reference, likea:

public interface IDrive {
// common definitions
}

// later in character script

private IDrive drive;

void OnEnterVehicle(GameObject driveObject) {
    drive = driveObject.GetComponent<IDrive>();
}

I’m using different scripts because some vehicles have completely different methods of control and set up and such, kinda like Kirby Air Ride, and thank you, I’ll try this!

I advice you to generalize all vehicle controls to common interface because if you don’t, it will be a pain to script logic for every vehicle-character combination possible. And even if this is not possible, you may create IDrive - IDriver pair so IDrive just have two methods setDriver(IDriver driver) and clearDriver(). This way you can easily put any driver into any vehicle, implement different drivers in different scripts. This will make your code much cleaner and work much easier.

Recently I wrote something similar to what you’re trying to do, except in my case it was AI behaviors instead of vehicles. One thing I just discovered that might be helpful to you (and is just cool, besides) is that getComponent() will still return a component even if the component is actually derived from class T instead of being type T itself. So in my case, I made a base class called MonsterAI with an interface of virtual functions: functions like AIinit and AIstep, and so on (edit: and by “interface”, I mean in the general sense, not the C# language feature). Then I make multiple derived classes, like MonsterAI_seekPlayer and MonsterAI_followPath and so on that overrides the the interface functions (plus whatever additional functions and data it needs).

My Monster class just does a MonsterAI MyAI=getComponent<MonsterAI>
and calls the MyAI.AIstep() and such functions during it’s routine, and the derived class’s implementation is called. That means I can just drag any of my AI components that I want and it will just automatically work.

The way I have it implemented, there can only be one of these components on the object though. Theoretically, it should be easy to do more than one, but I haven’t tested it.

Edit: It occurs to me I could probably have done it cleaner using Unity Events. Oh well, when I make the exciting sequel to my current game I’ll try it. :slight_smile: You may want to research Unity Events. It’s a great feature when you want two objects to interact with each other, without either object’s scripts having to commit to what type the other will be.