###実現したいこと
音ゲーでNotesを生成する
###発生している問題・エラーメッセージ
エラーメッセージ IndexOutOfRangeException: Array index is out of range. GameManager.SpawnNotes (Int32 num) (at Assets/script/GameManager.cs:65) GameManager.CheckNextNotes () (at Assets/script/GameManager.cs:58) GameManager.Update () (at Assets/script/GameManager.cs:40)
###該当のソースコード
C#
1using UnityEngine; 2using System.Collections; 3using System.IO; 4using System; 5using UnityEngine.UI; 6 7public class GameManager : MonoBehaviour 8{ 9 10 public GameObject[] notes; 11 private float[] _timing; 12 private int[] _lineNum; 13 14 public string filePass; 15 private int _notesCount = 0; 16 17 private AudioSource _audioSource; 18 private float _startTime = 0; 19 20 public float timeOffset = -1; 21 22 private bool _isPlaying = false; 23 public GameObject startButton; 24 25 public Text scoreText; 26 private int _score = 0; 27 28 void Start() 29 { 30 _audioSource = GameObject.Find("GameMusic").GetComponent<AudioSource>(); 31 _timing = new float[1024]; 32 _lineNum = new int[1024]; 33 LoadCSV(); 34 } 35 36 void Update() 37 { 38 if (_isPlaying) 39 { 40 CheckNextNotes(); 41 scoreText.text = _score.ToString(); 42 } 43 44 } 45 46 public void StartGame() 47 { 48 startButton.SetActive(false); 49 _startTime = Time.time; 50 _audioSource.Play(); 51 _isPlaying = true; 52 } 53 54 void CheckNextNotes() 55 { 56 while (_timing[_notesCount] + timeOffset < GetMusicTime() && _timing[_notesCount] != 0) 57 { 58 SpawnNotes(_lineNum[_notesCount]); 59 _notesCount++; 60 } 61 } 62 63 void SpawnNotes(int num) 64 { 65 Instantiate(notes[num], 66 new Vector3(-4.0f + (2.0f * num), 10.0f, 0), 67 Quaternion.identity); 68 } 69 70 void LoadCSV() 71 { 72 int i = 0, j; 73 TextAsset csv = Resources.Load(filePass) as TextAsset; 74 StringReader reader = new StringReader(csv.text); 75 while (reader.Peek() > -1) 76 { 77 78 string line = reader.ReadLine(); 79 string[] values = line.Split(','); 80 _timing[i] = float.Parse(values[0]); 81 _lineNum[i] = int.Parse(values[1]); 82 i++; 83 } 84 } 85 86 float GetMusicTime() 87 { 88 return Time.time - _startTime; 89 } 90 91 public void GoodTimingFunc(int num) 92 { 93 Debug.Log("Line:" + num + " good!"); 94 Debug.Log(GetMusicTime()); 95 _score++; 96 } 97}
###試したこと
変数の確認等
###補足情報(言語/FW/ツール等のバージョンなど)
変数の確認は、どの時点のどの変数を確認されましたでしょうか。また、不具合の発生する条件を絞り込まれると、解決の手掛かりになるかと思います(全く音符が出現せずすぐエラーとなるのか、ある程度経つとエラーとなるのか、あるいは譜面を変えるとエラーの発生タイミングが変化するか...など)。
回答2件
あなたの回答
tips
プレビュー