2016-04-13 52 views
0

ゲーム内の店にはShop.csというスクリプトがあります。私はボタンをクリックすると、この奇妙なエラーが発生します。それは言う:Unity IndexOutOfRangeException私が解決できない

IndexOutOfRangeException: Array index is out of range. 
Shop+<Start>c__AnonStorey1.<>m__9() (at Assets/Scripts/Menu/Shop.cs:24) 
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:144) 
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:621) 
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:756) 
UnityEngine.Events.UnityEvent.Invoke() (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) 
UnityEngine.UI.Button.Press() (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) 
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) 
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) 
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) 
UnityEngine.EventSystems.EventSystem:Update() 

Btnsと武器は同じ長さです。ここ
が、これはnoobの質問であればごめんなさいShop.cs

using UnityEngine; 
using UnityEditor; 
using UnityEngine.UI; 

public class Shop : MonoBehaviour { 

    public Button[] btns; 
    public int money; 
    public Weapon[] weapons; 

    public int weaponLength; 

    void Start() 
    { 

     weapons = new Weapon[weaponLength]; 

     AddWeaponsToArray(); 

     for(int i = 0; i < btns.Length; i++) 
     { 
      btns[i].onClick.AddListener(delegate() 
      { 
       if (weapons[i] == null) 
        Debug.LogError("Not enough wpns!!!"); 
       else 
        BoughtSomething(weapons[i], btns[i]); 
      }); 
     } 


    } 

    void Update() 
    { 
     for (int i = 0; i < weapons.Length; i++) 
     { 
      Weapon _wpn = weapons[i]; 
      if (!_wpn.purchased) 
       _wpn.active = false; 
      if (_wpn.active) 
       btns[i].enabled = false; 
      if (!_wpn.purchased && _wpn.cost > money) 
       btns[i].enabled = false; 
     } 
    } 

    void AddWeaponsToArray() 
    { 
     weapons[0] = CreateWeapon.newWeapon("Roto", 1, 100, 0.5f, 50, 100, false, false); 
    } 

    void BoughtSomething(Weapon _wpn, Button _btn) 
    { 
     if (_wpn.purchased) 
     { 
      _wpn.active = true; 
     } 
     else 
     { 
      money -= _wpn.cost; 
      _wpn.purchased = true; 
      _wpn.active = true; 
     } 
    } 

} 

です。私が別のスクリプトを書くことを望むなら、私はそうするでしょう。また、私はあなたにインスペクタやシーンの画像を、この投稿に追加します。

enter image description here

+0

。たぶんbtns.Length> weapons.Length? – fillobotto

+0

私の新しい編集を見てください – salipshitz

+0

エラーはStore.csにありますが、そのファイルの名前を変更して買い物をしましたか? – Allen

答えて

1

変数がキャプチャされていないために発生する可能性があります。 Using the iterator variable of foreach loop in a lambda expression - why fails?

基本的には、あなたのインデックス変数への参照があるだけなので、forループで2に更新されると、2はデリゲートが呼び出されたときに返される値です。 btn [2]が範囲外です。

あなたはこれを行うことができるはずです:Startメソッドでは、あなたが同じ長さの2つの配列を持っていると仮定していて、「私」のインデックスでそれらの両方にアクセスしている

int capturedIndex = i; 
BoughtSomething(weapons[capturedIndex], btns[capturedIndex]); 
0

私の推測では、それがここにあるということです。コメントを参照してください。

for(int i = 0; i < btns.Length; i++) 
    { 
     btns[i].onClick.AddListener(delegate() 
     { 
      // Are btns & weapons the same length? 
      BoughtSomething(weapons[i], btns[i]); 
     }); 
    } 

//Safer 
for(int i = 0; i < btns.Length && i < weapons.Length; i++) { 
    var btn = btns[i]; 
    var weapon = weapons[i]; 
    if(btn == null || weapon == null) continue; 
    btn.onClick.AddListener(delegate() { 
     BoughtSomething(weapon, btn); 
    }); 
} 
+0

はい。彼らは両方とも1 – salipshitz

+0

どうすれば100%確実に実際にチェックすることができますか(私は Stephen

+0

私の新しい編集を見てください – salipshitz

関連する問題