我有武器统计的自定义类, 然后我有一个武器经理类,其中包含所有他们的统计数据的武器列表(不切实际的数据只是为了让它快速运行以进行测试)
继承我的武器数据类
using UnityEngine;
using System.Collections;
public class WeaponData {
public string Name {get; set;}
public float Power {get; set;}
public float Range {get; set;}
public float Accuracy {get; set;}
public float Mobility { get; set;}
public int MaxAmmo{get; set;}
public int CurrentAmmo{get;set;}
public string Model {get; set;}
//
public WeaponData(){
Name = "";
Range = 0f;
Power = 0f;
Accuracy = 0f;
Mobility = 0f;
MaxAmmo = 0;
CurrentAmmo = 0;
Model = "";
}
}
继承我的武器经理班
using UnityEngine;
using System.Collections;
public class WeaponMgr : WeaponData{
public static WeaponMgr WM;
public WeaponData Weapon;
public WeaponMgr(){
Weapon = new WeaponData();
}
//PRIMARY
public WeaponData M4(){
Weapon = new WeaponData();
Weapon.Name = "M4 Carbine";
Weapon.Accuracy = 0.6f;
Weapon.Mobility = 0.5f;
Weapon.Power = 0.5f;
Weapon.Range = 0.6f;
Weapon.Model = "Aslt_M4Carbine";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData M16(){
Weapon = new WeaponData();
Weapon.Name = "M16";
Weapon.Accuracy = 0.4f;
Weapon.Mobility = 0.6f;
Weapon.Power = 0.6f;
Weapon.Range = 0.5f;
Weapon.Model = "Aslt_M16";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData Ak47(){
Weapon = new WeaponData();
Weapon.Name = "Ak 47";
Weapon.Accuracy = 0.5f;
Weapon.Mobility = 0.4f;
Weapon.Power = 0.5f;
Weapon.Range = 0.4f;
Weapon.Model = "Aslt_Ak47";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
//SMG
public WeaponData MP5(){
Weapon = new WeaponData();
Weapon.Name = "MP5";
Weapon.Accuracy = 0.3f;
Weapon.Mobility = 0.7f;
Weapon.Power = 0.3f;
Weapon.Range = 0.4f;
Weapon.Model = "Smg_MP5";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData P90(){
Weapon = new WeaponData();
Weapon.Name = "P90";
Weapon.Accuracy = 0.4f;
Weapon.Mobility = 0.8f;
Weapon.Power = 0.3f;
Weapon.Range = 0.3f;
Weapon.Model = "Smg_P90";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData Ak74(){
Weapon = new WeaponData();
Weapon.Name = "AK 74";
Weapon.Accuracy = 0.2f;
Weapon.Mobility = 0.6f;
Weapon.Power = 0.3f;
Weapon.Range = 0.3f;
Weapon.Model = "Smg_AK74";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData Scorpion(){
Weapon = new WeaponData();
Weapon.Name = "Scorpion";
Weapon.Accuracy = 0.2f;
Weapon.Mobility = 0.8f;
Weapon.Power = 0.4f;
Weapon.Range = 0.2f;
Weapon.Model = "Smg_Scorpion";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
//Sniper
public WeaponData BarretRifle(){
Weapon = new WeaponData();
Weapon.Name = ".50 Caliber Barret Rifle";
Weapon.Accuracy = 0.9f;
Weapon.Mobility = 0.3f;
Weapon.Power = 1.0f;
Weapon.Range = 1.0f;
Weapon.Model = "Snp_BarretRifle";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData Intervention(){
Weapon = new WeaponData();
Weapon.Name = "Intervention";
Weapon.Accuracy = 1.0f;
Weapon.Mobility = 0.3f;
Weapon.Power = 1.0f;
Weapon.Range = 1.0f;
Weapon.Model = "Snp_Intervention";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData RemingtonRifle(){
Weapon = new WeaponData();
Weapon.Name = "Remington 700";
Weapon.Accuracy = 0.8f;
Weapon.Mobility = 0.2f;
Weapon.Power = 0.7f;
Weapon.Range = 0.9f;
Weapon.Model = "Snp_RemingtonRifle";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
//Shot Gun
public WeaponData DoubleBarrel(){
Weapon = new WeaponData();
Weapon.Name = "Double Barrel Shotgun";
Weapon.Accuracy = 0.1f;
Weapon.Mobility = 0.3f;
Weapon.Power = 0.9f;
Weapon.Range = 0.2f;
Weapon.Model = "Shg_DoulbeBarrel";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData TwelveGauge(){
Weapon = new WeaponData();
Weapon.Name = "12. Gauge";
Weapon.Accuracy = 0.2f;
Weapon.Mobility = 0.4f;
Weapon.Power = 0.8f;
Weapon.Range = 0.1f;
Weapon.Model = "Shg_12Gauge";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
//Pistol
public WeaponData NineMM(){
Weapon = new WeaponData();
Weapon.Name = "9mm";
Weapon.Accuracy = 0.5f;
Weapon.Mobility = 0.9f;
Weapon.Power = 0.3f;
Weapon.Range = 0.4f;
Weapon.Model = "Pst_9mm";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData Glock(){
Weapon = new WeaponData();
Weapon.Name = "Glock";
Weapon.Accuracy = 0.6f;
Weapon.Mobility = 0.8f;
Weapon.Power = 0.4f;
Weapon.Range = 0.3f;
Weapon.Model = "Pst_Glock";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData DesertEagle(){
Weapon = new WeaponData();
Weapon.Name = "DesertEagle";
Weapon.Accuracy = 0.4f;
Weapon.Mobility = 0.7f;
Weapon.Power = 0.7f;
Weapon.Range = 0.5f;
Weapon.Model = "Pst_DesertEagle";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
public WeaponData Magnum(){
Weapon = new WeaponData();
Weapon.Name = "Magnum Revolver";
Weapon.Accuracy = 0.3f;
Weapon.Mobility = 0.6f;
Weapon.Power = 0.8f;
Weapon.Range = 0.4f;
Weapon.Model = "Pst_Magnum";
Weapon.MaxAmmo = 20;
Weapon.CurrentAmmo =20;
return Weapon;
}
//SECONDARY
//MELEE
public WeaponData Fist(){
Weapon = new WeaponData();
Weapon.Name = "Fist";
Weapon.Accuracy = 1.0f;
Weapon.Mobility = 1.0f;
Weapon.Power = 0.1f;
Weapon.Range = 0.02f;
Weapon.Model = "Mle_Fist";
return Weapon;
}
public WeaponData Knife(){
Weapon = new WeaponData();
Weapon.Name = "Knife";
Weapon.Accuracy = 0.9f;
Weapon.Mobility = 0.9f;
Weapon.Power = 0.3f;
Weapon.Range = 0.05f;
Weapon.Model = "Mle_Knife";
return Weapon;
}
public WeaponData Hatchet(){
Weapon = new WeaponData();
Weapon.Name = "Hatchet";
Weapon.Accuracy = 0.7f;
Weapon.Mobility = 0.5f;
Weapon.Power = 0.6f;
Weapon.Range = 0.05f;
Weapon.Model = "Mle_Hatchet";
return Weapon;
}
public WeaponData Katana(){
Weapon = new WeaponData();
Weapon.Name = "Katana";
Weapon.Accuracy = 0.6f;
Weapon.Mobility = 0.7f;
Weapon.Power = 0.7f;
Weapon.Range = 0.1f;
Weapon.Model = "Mle_Katana";
return Weapon;
}
}
现在当我尝试设置我声明的玩家主要武器变量时 public WeaponData PrimaryWeapon;
然后尝试设置等于... M4
Player.PrimaryWeapon = new WeaponManager()。M4;
它不起作用我得到错误 无法将方法组“M4转换为非delgate类型WeaponData。
我在这里做错了什么......?
我仍然是C#的新手 并且用很少和非常小的例子自学。
答案 0 :(得分:3)
方法名称本身就是一个"方法组"。它表示具有该名称的重载组。它可以用于委托推理,但是如果你想调用方法,你必须添加括号,即使没有任何参数要传递。
尝试:
Player.PrimaryWeapon = new WeaponManager().M4();
在修复方法调用后,请参阅另一个答案,了解您仍然需要处理的错误的解释(注意所有方法中都存在问题,而不仅仅是M4()
)。
答案 1 :(得分:2)
您没有为结构命名。所以没有变量。
public WeaponData M4(){
Weapon m4 = new WeaponData();
m4.Name = "M4 Carbine";
m4.Accuracy = 0.6f;
m4.Mobility = 0.5f;
m4.Power = 0.5f;
m4.Range = 0.6f;
m4.Model = "Aslt_M4Carbine";
m4.MaxAmmo = 20;
m4.CurrentAmmo =20;
return m4;
}