2016-05-08 12 views
0

私は同じクラスを2つ持っています.1つは主武器用で、もう1つは副武器です。ここに私のクラスである:オブジェクトを作成できませんC#

public class weapon : MonoBehaviour { 

public string Name { get; set; } 
public float Damages { get; set; } 
public float FireRate { get; set; } 
public float Range { get; set; } 
public float BulletSpeed { get; set; } 
public bool isOn { get; set; } 

public weapon(string name, float damages, float fireRate, float range, float bulletSpeed, bool ison) { 

    this.Name = name; 
    this.Damages = damages; 
    this.FireRate = fireRate; 
    this.Range = range; 
    this.BulletSpeed = bulletSpeed; 
    this.isOn = ison; 
    } 
} 

public class weapon1 : MonoBehaviour { 

public string Name { get; set; } 
public float Damages { get; set; } 
public float FireRate { get; set; } 
public float Range { get; set; } 
public float BulletSpeed { get; set; } 
public bool isOn { get; set; } 

public weapon1(string name, float damages, float fireRate, float range, float bulletSpeed, bool ison) { 

    this.Name = name; 
    this.Damages = damages; 
    this.FireRate = fireRate; 
    this.Range = range; 
    this.BulletSpeed = bulletSpeed; 
    this.isOn = ison; 
    } 
} 

私は別のスクリプトにしたい、私は「F」を押すと、武器はそれぞれ自分のスペックを取るが、それは二番目に名前を与えることで立ち往生、ここに私のスクリプトがあることそのスペック彼らに与える:

void Update() { 

    if (Input.GetKeyDown (KeyCode.F)) { 

     GetComponent<weapon>().Name = "Rien"; 
     GetComponent<weapon>().Damages = 0; 
     GetComponent<weapon>().FireRate = 0; 
     GetComponent<weapon>().Range = 0; 
     GetComponent<weapon>().BulletSpeed = 0; 
     GetComponent<weapon>().isOn = true; 

     Debug.Log (GetComponent<weapon>().Name); 

     GetComponent<weapon1>().Name = "Rien1"; //stuck here... :'(
     GetComponent<weapon1>().Damages = 0; 
     GetComponent<weapon1>().FireRate = 0; 
     GetComponent<weapon1>().Range = 0; 
     GetComponent<weapon1>().BulletSpeed = 0; 
     GetComponent<weapon1>().isOn = false; 

     Debug.Log(GetComponent<weapon1>().Name); 
    } 

} 

私は取得しています

+3

このように、まったく同じ複数のクラスを作成しないでください。あるクラスを作成し、それを必要なだけ何回もインスタンス化し、メンバーを変更して各インスタンスを一意にします。 –

+1

さて、 'GetComponent ()'は 'null'を返しました。あなたは 'weapon'と' weapon1'スクリプトの両方がSAMEオブジェクトにあると確信していますか?また、まったく同じコードで、別の名前の2つのクラスを持つのは悪いスタイルだとわかります。それを変更して維持するのは痛いでしょう。 1つの「武器」スクリプトを実行し、プライマリとセカンダリの武器を区別する別のプロパティを追加します。 –

+0

あなたは私がどのようにすべきか考えていますか? – Ay0m3

答えて

1

あなたは、いくつかを作っている事前に 感謝を「オブジェクト参照がオブジェクトインスタンスに設定されていない」と私は両方の武器のためにまったく同じことをしました基本的なプログラミングエラーだけでなく、いくつかの基本的なU特定のエラーがあります。コメントの中で述べたように、まったく同じクラスがたくさんあるべきではありません。覚えているのはnot repeat your selfです。

次に、MonoBehaviourから継承していますが、フィールドを設定するコンストラクターメソッドを定義しています。これを団結させないでください。インスペクタでfeildを公開できるようにするか、[SerializeField]属性をプライベートメンバ変数に追加します。あなたは、オブジェクトが作成されたとき、その後Start()方法でこれを入れてやりたいものがある場合:今

public Weapon : MonoBehviour 
{ 
    public string name; //set these in the inspector for each weapon 
    public float damage; 
    //etc 

    // in Unity you use the Start method for mono behaviours. NOT the constructor method 
    void Start() 
    { 
     //Do things when the object is created 
    } 
} 

を、それはあなたがあなたの質問から、あなたのプレイヤーに武器を追加することなく、何がすべき方法は明らかではありません階層内のプレーヤーオブジェクトに子としてゲームオブジェクトを追加します。各武器に対してこれを行い、各武器に武器スクリプトを追加します。次に、インスペクタで自分が使用可能にしたフィールドを変更します。プレーヤーが現在持っている武器への参照をプレイヤー上のスクリプトを介して取得し、キーを押したときにそれらの値を気に入ってください。

+1

私は完全に1つのクラスを教えて私のスクリプトを書き直し、非常に多く:D – Ay0m3

+0

非常に良い、聞いてうれしい。団結して始めたばあいなら、[official tutorials](https://unity3d.com/learn/tutorials/topics/scripting)をご覧ください。彼らは基本的に非常に明確です!幸運の最高。 –

関連する問題