从C#中的另一个脚本更改对象var

时间:2016-05-07 12:18:44

标签: c#

我试图从另一个脚本更改对象var。 我有脚本A:

public class weapon : MonoBehaviour {

public weapon weaponOn;
private string Name;
private float Damages;
private float FireRate;

public weapon(string name, float damages, float fireRate) {

    this.Name = name;
    this.Damages = damages;
    this.FireRate = fireRate;

}

void Start () {

    weapon weaponOn = new weapon ("Rien", 0, 0);
    Debug.Log (weaponOn.Name);

我正在尝试更改脚本B中'weaponOn'的名称,如:

public class buyWeapon : MonoBehaviour {
public static weapon weaponn;

void Start () {

    weaponn = gameObject.GetComponent<weapon> ().weaponOn;
        }

void Update () {

    if (Input.GetKeyDown (KeyCode.F)) {
        weaponn.Name = "New Name"; //Doesn"t contain a definition for Name

        }
    }
}

我很抱歉,如果你觉得这很容易,但我迷失在'静态'var'公共/私人'这样的东西,我是C#的新手,所以请帮忙。 提前致谢:D

1 个答案:

答案 0 :(得分:0)

Nameprivate。设为public

public string Name;