Access script function without knowing the name of the script?

How would I go about accessing a script function while not knowing the scripts name? I’d like to be able to add a script, or a GameObject that has a script, to the character in the inspector(on a script) and not have to know the name of the script. The only other way I have found is by using a BroadcastMessage but it appears rather slow and I feel like I should just be able to drag and drop in the inspector and declare the script so that I can access it.

Basically, I’m trying to add these “weapons” to my characters by just dragging and dropping them on the different characters. It wouldn’t be too hard to go into each character and add the names of the scripts(weapons) but there just has to be a way to do this, plus if there is, it would probably deepen my understanding of Unity.

Thanks for your time.

Why not a base class called Weapon and them a bunch of child classes that inherit from it?

public class Weapon : MonoBehaviour
	{
		public virtual void Shoot()
		{
			Debug.Log("Pew Pew");
		}
		
		public virtual void Reload()
		{
			Debug.Log("Lock and Loaded");
		}
	}
	
	public class LaserGun : Weapon
	{
		public override void Shoot ()
		{
			// Laser shooting logic
		}
		
		override void Reload ()
		{
			// Laser gun reloaing logic
		}
	}
	
	public class Player : MonoBehaviour
	{
		public List<Weapon> weaponList = new List<Weapon>();
		public int selectedWeapon;
		
		void Update()
		{
			if(Input.GetKeyDown(KeyCode.A))
			{
				weaponList[selectedWeapon].Shoot();
			}
		}
	}

The way Unity works, everything is encapsulated in a great, invisible namespace. Thus, your classes in Weapon.cs can be seen in the Player.cs behavior.

As for telling a script on a parent object about some object that you’ve just equipped, you have some options. A GUI script could do that directly, provided your Equipment variable is public.

If you’re equipping items by running over them, you get into some interesting scenarios, like having the script that does the picking up on the same level as your collider; when your collider senses that a collided object is an Equipment, tells the Player behavior to set its Equipment to that instance of the Equipment you just picked up.

Messages are another valid and good way to do this, but I personally try to avoid them as much as possible, out of personal preference. I would count a Messaging setup as a last resort with this type of functionality, because a direct reference can probably do the job much more cleanly.