2016-05-08 124 views
0

我有兩個相同的類,一個用於主要武器,另一個用於第二個。這裏是我的課:無法創建對象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'。你確定「武器」和「武器1」腳本都在SAME對象上嗎?另外:我覺得有兩個完全相同的代碼但名稱不同的類是不好的風格。對它們進行修改和維護是很痛苦的。只需做一個「武器」腳本並添加另一個屬性來區分主要和次要武器。 –

+0

你知道我應該怎麼做嗎? – Ay0m3

回答

1

你做一些基本的編程錯誤以及一些基本的U.具體的錯誤。正如評論中所說,你不應該有很多完全相同的類。請記住not repeat your self

然後,您從MonoBehaviour繼承,但正在定義一個構造函數方法來設置您的字段。不要統一這樣做。通過將檢查器中的實體公開,或者將[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 
    } 
} 

現在,你是如何從你的問題將武器你的球員,但目前還不清楚是什麼你應該 do是將一個遊戲對象作爲一個孩子添加到層次結構中的玩家對象。爲每件武器做這件事,然後將你的武器腳本加入每件武器。然後在檢查員中更改您提供給自己的字段。然後,您可以通過播放器上的腳本獲得玩家當前擁有的武器的參考信息,並在您按下某個鍵時按照您的值操作。

+1

我完全重寫了我的腳本,只讓一個班級,我會記下你所說的話謝謝非常:D – Ay0m3

+0

非常好,很高興聽到。如果你剛剛開始統一,那麼請查看[官方教程](https://unity3d.com/learn/tutorials/topics/scripting)。基礎知識非常清楚!祝你好運。 –