質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.35%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

オブジェクト

オブジェクト指向において、データとメソッドの集合をオブジェクト(Object)と呼びます。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2659閲覧

Unity音ゲー制作にてオブジェクトが生成されない

NIRANEZUMI

総合スコア1

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

オブジェクト

オブジェクト指向において、データとメソッドの集合をオブジェクト(Object)と呼びます。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/11/07 23:55

前提・実現したいこと

Unityで初めてのゲーム制作で音楽ゲーム(音ゲー)を制作しています。
現在、Jsonファイルを読み込んでオブジェクトを生成するという工程を行っていますがそこで問題が起きています。

発生している問題

読み込みはちゃんとできているはずですがオブジェクトが一つしか生成されません。

該当のソースコード

C#

1using UnityEngine; 2using System.Collections; 3using System.IO; 4using System; 5using UnityEngine.UI; 6 7public class GameManager : MonoBehaviour { 8 [Serializable] public class InputJson 9 { 10 public string name; 11 public int BPM; 12 public Note[] Notes; 13} 14[Serializable] public class Note 15 { 16 public int LPB; 17 public int num; 18 public int block; 19 public int type; 20} 21 public GameObject[] notes; 22 private float[] _timing; 23 private int[] _lineNum; 24 public string filePass; 25 private int _notesCount = 0; 26 public float timing; 27 private AudioSource _audioSource; 28 private float _startTime = 0; 29 public float timeOffset = 0; 30 private bool _isPlaying = false; 31 public GameObject startButton; 32 33 void Start(){ 34 _audioSource = GameObject.Find ("GameMusic").GetComponent<AudioSource> (); 35 _timing = new float[1024]; 36 _lineNum = new int[1024]; 37 LoadCSV (); 38 } 39 40 void Update () { 41 if (_isPlaying) { 42 CheckNextNotes (); 43 } 44 45 } 46 47 public void StartGame(){ 48 startButton.SetActive (false); 49 _startTime = Time.time; 50 _audioSource.Play (); 51 _isPlaying = true; 52 } 53 54 void CheckNextNotes(){ 55 while (_timing [_notesCount] + timeOffset < GetMusicTime () && _timing [_notesCount] != 0) { 56 SpawnNotes (_lineNum[_notesCount]); 57 _notesCount++; 58 59 } 60 } 61 62 void SpawnNotes(int num){ 63 Instantiate (notes[num], 64 new Vector3 (-4.0f + (2.0f*num), 10.0f, 0), 65 Quaternion.identity); 66 } 67 68 void LoadCSV(){ 69 string json = Resources.Load (filePass).ToString(); 70 InputJson inputJson = JsonUtility.FromJson<InputJson>(json); 71 Debug.Log(inputJson.name); 72 int i=0; 73 for(int j=0;j < inputJson.Notes.Length; j++){ 74 timing=((float)inputJson.Notes[j].num*4+inputJson.Notes[j].LPB)/100; 75 _timing [i] = timing; 76 _lineNum [i] = inputJson.Notes[j].block; 77 } 78 i++; 79 } 80 float GetMusicTime(){ 81 return Time.time - _startTime; 82 } 83}

Json

1{ 2 "name" : "1F√", 3 "BPM" : "130", 4 "offset" : "4500", 5 "Notes" :[ 6 {"LPB":"3","num":"108","block":"0","type":"1","notes":[]}, 7 {"LPB":"3","num":"108","block":"3","type":"1","notes":[]}, 8 {"LPB":"4","num":"134","block":"1","type":"1","notes":[]}, 9 {"LPB":"4","num":"136","block":"0","type":"1","notes":[]}, 10 {"LPB":"4","num":"138","block":"1","type":"1","notes":[]}, 11 {"LPB":"4","num":"140","block":"2","type":"1","notes":[]}, 12 {"LPB":"4","num":"142","block":"3","type":"1","notes":[]}, 13 {"LPB":"4","num":"146","block":"1","type":"1","notes":[]}, 14 {"LPB":"4","num":"148","block":"0","type":"1","notes":[]}, 15 {"LPB":"4","num":"148","block":"3","type":"1","notes":[]}, 16 {"LPB":"4","num":"150","block":"2","type":"1","notes":[]}, 17 {"LPB":"4","num":"152","block":"0","type":"1","notes":[]}, 18 {"LPB":"4","num":"152","block":"3","type":"1","notes":[]}, 19 {"LPB":"4","num":"154","block":"1","type":"1","notes":[]}, 20 {"LPB":"4","num":"156","block":"0","type":"1","notes":[]}, 21 {"LPB":"4","num":"156","block":"3","type":"1","notes":[]}, 22 {"LPB":"4","num":"158","block":"2","type":"1","notes":[]}, 23 {"LPB":"4","num":"160","block":"3","type":"1","notes":[]}, 24 {"LPB":"4","num":"162","block":"0","type":"1","notes":[]}, 25 {"LPB":"4","num":"164","block":"3","type":"1","notes":[]}, 26 {"LPB":"4","num":"166","block":"1","type":"1","notes":[]}, 27 {"LPB":"4","num":"168","block":"0","type":"1","notes":[]}, 28 {"LPB":"4","num":"170","block":"1","type":"1","notes":[]}, 29 {"LPB":"4","num":"172","block":"2","type":"1","notes":[]}, 30 {"LPB":"4","num":"174","block":"3","type":"1","notes":[]}, 31 {"LPB":"4","num":"176","block":"1","type":"1","notes":[]}, 32 {"LPB":"4","num":"176","block":"2","type":"1","notes":[]}, 33 {"LPB":"4","num":"178","block":"0","type":"1","notes":[]}, 34 {"LPB":"4","num":"180","block":"1","type":"1","notes":[]}, 35 {"LPB":"4","num":"180","block":"2","type":"1","notes":[]}, 36 {"LPB":"4","num":"182","block":"3","type":"1","notes":[]}, 37 {"LPB":"4","num":"184","block":"1","type":"1","notes":[]}, 38 {"LPB":"4","num":"184","block":"2","type":"1","notes":[]}, 39 {"LPB":"4","num":"186","block":"0","type":"1","notes":[]}, 40 {"LPB":"4","num":"188","block":"1","type":"1","notes":[]}, 41 {"LPB":"4","num":"188","block":"2","type":"1","notes":[]}, 42 {"LPB":"4","num":"190","block":"3","type":"1","notes":[]}, 43 {"LPB":"4","num":"192","block":"3","type":"1","notes":[]}, 44 {"LPB":"4","num":"194","block":"0","type":"1","notes":[]}, 45 {"LPB":"4","num":"196","block":"3","type":"1","notes":[]}, 46 {"LPB":"4","num":"198","block":"1","type":"1","notes":[]}, 47 {"LPB":"4","num":"200","block":"0","type":"1","notes":[]}, 48 {"LPB":"4","num":"202","block":"2","type":"1","notes":[]}, 49 {"LPB":"4","num":"704","block":"0","type":"1","notes":[]}, 50 {"LPB":"4","num":"206","block":"2","type":"1","notes":[]}, 51 {"LPB":"4","num":"208","block":"0","type":"1","notes":[]}, 52 {"LPB":"4","num":"208","block":"3","type":"1","notes":[]}, 53 {"LPB":"4","num":"210","block":"2","type":"1","notes":[]}, 54 {"LPB":"4","num":"212","block":"0","type":"1","notes":[]}, 55 {"LPB":"4","num":"212","block":"3","type":"1","notes":[]}, 56 {"LPB":"4","num":"214","block":"1","type":"1","notes":[]}, 57 {"LPB":"4","num":"216","block":"0","type":"1","notes":[]}, 58 {"LPB":"4","num":"216","block":"3","type":"1","notes":[]}, 59 {"LPB":"4","num":"218","block":"2","type":"1","notes":[]}, 60 {"LPB":"4","num":"220","block":"0","type":"1","notes":[]}, 61 {"LPB":"4","num":"220","block":"3","type":"1","notes":[]}, 62 {"LPB":"4","num":"222","block":"1","type":"1","notes":[]}, 63 {"LPB":"4","num":"224","block":"3","type":"1","notes":[]}, 64 {"LPB":"4","num":"226","block":"0","type":"1","notes":[]}, 65 {"LPB":"4","num":"228","block":"3","type":"1","notes":[]}, 66 {"LPB":"4","num":"230","block":"1","type":"1","notes":[]}, 67 {"LPB":"4","num":"232","block":"0","type":"1","notes":[]}, 68 {"LPB":"4","num":"234","block":"1","type":"1","notes":[]}, 69 {"LPB":"4","num":"236","block":"2","type":"1","notes":[]}, 70 {"LPB":"4","num":"238","block":"3","type":"1","notes":[]}, 71 {"LPB":"4","num":"240","block":"1","type":"1","notes":[]}, 72 {"LPB":"4","num":"240","block":"2","type":"1","notes":[]}, 73 {"LPB":"4","num":"242","block":"3","type":"1","notes":[]}, 74 {"LPB":"4","num":"244","block":"1","type":"1","notes":[]}, 75 {"LPB":"4","num":"244","block":"2","type":"1","notes":[]}, 76 {"LPB":"4","num":"246","block":"0","type":"1","notes":[]}, 77 {"LPB":"4","num":"248","block":"1","type":"1","notes":[]}, 78 {"LPB":"4","num":"248","block":"2","type":"1","notes":[]}, 79 {"LPB":"4","num":"250","block":"3","type":"1","notes":[]}, 80 {"LPB":"4","num":"252","block":"1","type":"1","notes":[]}, 81 {"LPB":"4","num":"252","block":"2","type":"1","notes":[]}, 82 {"LPB":"4","num":"254","block":"0","type":"1","notes":[]}, 83 {"LPB":"4","num":"256","block":"3","type":"1","notes":[]}, 84 {"LPB":"4","num":"258","block":"0","type":"1","notes":[]}, 85 {"LPB":"4","num":"260","block":"3","type":"1","notes":[]}, 86 {"LPB":"4","num":"262","block":"2","type":"1","notes":[]}, 87 {"LPB":"4","num":"264","block":"0","type":"1","notes":[]}, 88 {"LPB":"4","num":"266","block":"2","type":"1","notes":[]} 89 ]} 90

試したこと

参考にしている記事
[Unity]シンプルな音ゲーの作り方 part2 ゲーム製作
UnityでJSONファイルを読み込むメモ
そのほかにもネットで調べたり、自分が持っている参考書を見ました。

補足情報(FW/ツールのバージョンなど)

Unity 2020.1.5f1
NoteEditor
【Unity】音ゲーの譜面エディタ「NoteEditor」紹介

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YAmaGNZ

2020/11/08 00:02

Debug.Logなどで実際に動作した時の変数の値やどこを実行したのかなどデバッグはされたのでしょうか?
NIRANEZUMI

2020/11/08 00:14 編集

しました。 ちゃんと読み込まれてはいました。 ネスト化しているところも問題ないです
YAmaGNZ

2020/11/08 00:45

なのであればCheckNextNotesのif文の中が実行されていないとかSpawnNotesにてインスタンスが生成されているはずなのに表示されないとか具体的な問題点が分かるかと思いますので、そういった情報を書かれるべきかと思います。
NIRANEZUMI

2020/11/08 00:56 編集

なるほど... Unity初心者なのであまり理解していませんでした。 勉強不足でした。
guest

回答1

0

ベストアンサー

LoadCSVにてiをインクリメントしている場所を間違えていませんか?

C#

1void LoadCSV(){ 2 string json = Resources.Load (filePass).ToString(); 3 InputJson inputJson = JsonUtility.FromJson<InputJson>(json); 4 Debug.Log(inputJson.name); 5 int i=0; 6 7 for(int j=0;j < inputJson.Notes.Length; j++){ 8 timing=((float)inputJson.Notes[j].num*4+inputJson.Notes[j].LPB)/100; 9 _timing[i] = timing; 10 _lineNum[i] = inputJson.Notes[j].block; 11 } 12 i++; 13}

このようにforの外でiを加算しているので、_timingなどの配列に値をセットするときに添え字が変化していないことになります。

投稿2020/11/08 01:06

YAmaGNZ

総合スコア10489

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

NIRANEZUMI

2020/11/08 01:17

修正されました。 何から何までありがとうございます! もう少しUnityを勉強します!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.35%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問