0

I'm wondering how I could get a String Variable (CurrentWeapon), to work like a Class reference (not sure how I can explain, as I am quite the noob, as you probably understand) to get different class variables from different classes depending on what weapon I have selected. The following script is a class where I have stored all the properties and stats for a weapon.

 public class Stats_SMG : MonoBehaviour {
     public static string Name = "SMG";
     public static int Damage = 20;
     public static float FireRate = 0.3f;
     public static int Magazine = 25;
     public static int ReserveMagazines = 2;
 }

The following script is how I try to "dynamically" access my weapons stat-classes (assume that I have multiple of the scripts above with different names and properties to differnet weapons, etc)

public class Shooting : MonoBehaviour {

 Stats_SMG smg;
 Stats_AssaultRifle ar;

 public string CurrentWeapon;


 void Start() {
     CurrentWeapon = smg;
     Debug.Log (CurrentWeapon.Damage);
 }

The above is just a quick sample of what I have to (hopefully) explain to you the problem that I have. Sorry if any sentences or words are misspelled or weird, as English is not my first language.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
McPluffel
  • 1
  • 1
  • 3
    It sounds to me like `CurrentWeapon` should be a `Weapon`, not a `string`... (And ideally, get rid of all these public fields, too...) – Jon Skeet Apr 07 '15 at 10:04

1 Answers1

1

What I'd do instead is that my weapon will have assigned script with stats to simply do:

public class Shooting : MonoBehaviour {

public Weapon currentWeapon;


void Start() {
    currentWeapon = smg; // Get somehow the currentWeapon object
    WeaponStats stats = currentWeapon.GetComponent<WeaponStats>();
    Debug.Log (stats.Damage);
}
Michael Czolko
  • 2,698
  • 2
  • 15
  • 25