I have a script called UpgradeController.cs with a variety of methods such as:
public void UpgradeSpeed() {//...}
public void UpgradeDamage() {// ... }
etc.
I want to know if I can create a separate object (prefab) that has a spot in the Unity Inspector where I can assign one of these functions in UpgradeController to this prefab.
Basically, it is a dropped item, that when you pick up, it upgrades your character. Instead of creating a separate script for every type of upgrade, I would like to just assigned the method that the prefab should call when picked up in the inspector.
The short answer is no, but there are workarounds to get you equivalent functionality. For example, you could use an enum to show the choice in the editor, and then have a single ‘Upgrade’ function that delegates to each different upgrade based on the enum;
public enum UpgradeType
{
Speed,
Damage
}
public UpgradeType upgradeType;
public void Upgrade ()
{
switch (upgradeType)
{
case UpgradeType.Speed:
UpgradeSpeed ();
break;
case UpgradeType.Damage:
UpgradeDamage ();
break;
}
}
private void UpgradeSpeed ();
private void UpgradeDamage ();
From there, you would just call ‘Upgrade’ in place of the other functions.