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

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

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

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

Unity

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

Q&A

解決済

1回答

1529閲覧

Unity クリックした場所にメッシュを生成したい エラー

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

Unity

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

0グッド

0クリップ

投稿2018/07/26 18:29

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Click1{ 6 7 public enum Type { Water } 8 public Type type; 9 10 public Click1(Type type) 11 { 12 this.type = Type.Water; 13 } 14} 15

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System; 5 6public class Click2 : MonoBehaviour { 7 8 public static Click2 instance; 9 10 public Material material; 11 12 public Click1[,] tiles; 13 14 private int i; 15 private int j; 16 17 // Use this for initialization 18 void Awake () { 19 instance = this; 20 } 21 22 private void Start() 23 { 24 tiles = new Click1[i, j]; 25 } 26 27 // Update is called once per frame 28 void Update () { 29 if (Input.GetMouseButtonDown(0)) 30 { 31 Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); 32 i = (int)pos.x; 33 j = (int)pos.y; 34 tiles[i, j] = new Click1(Click1.Type.Water); 35 GenerateMesh(i, j); 36 } 37 } 38 39 /*void CreateTiles() 40 { 41 tiles[i, j] = new Click1(Click1.Type.Water); 42 GenerateMesh(i, j); 43 }*/ 44 45 void GenerateMesh(int x, int y) 46 { 47 48 Click3 data = new Click3(i, j); 49 50 GameObject meshGO = new GameObject("CHUNK_" + x + "_" + y); 51 meshGO.transform.SetParent(this.transform); 52 53 MeshFilter filter = meshGO.AddComponent<MeshFilter>(); 54 MeshRenderer render = meshGO.AddComponent<MeshRenderer>(); 55 render.material = material; 56 57 Mesh mesh = filter.mesh; 58 59 mesh.vertices = data.vertices.ToArray(); 60 mesh.triangles = data.triangles.ToArray(); 61 mesh.uv = data.UVs.ToArray(); 62 } 63 64 public Click1 GetTileAt(int x, int y) 65 { 66 return tiles[i, j]; 67 } 68} 69

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Click3{ 6 7 public List<Vector3> vertices; 8 public List<Vector2> UVs; 9 public List<int> triangles; 10 11 public Click3(int x, int y) 12 { 13 vertices = new List<Vector3>(); 14 UVs = new List<Vector2>(); 15 triangles = new List<int>(); 16 17 CreateSquare(x, y); 18 } 19 20 void CreateSquare(int x, int y) 21 { 22 23 24 25 Click1 tile = Click2.instance.GetTileAt(x, y); 26 27 vertices.Add(new Vector3(x + 0, y + 0)); 28 vertices.Add(new Vector3(x + 1, y + 0)); 29 vertices.Add(new Vector3(x + 0, y + 1)); 30 vertices.Add(new Vector3(x + 1, y + 1)); 31 32 triangles.Add(vertices.Count - 1); 33 triangles.Add(vertices.Count - 3); 34 triangles.Add(vertices.Count - 4); 35 36 triangles.Add(vertices.Count - 2); 37 triangles.Add(vertices.Count - 1); 38 triangles.Add(vertices.Count - 4); 39 40 UVs.AddRange(Click4.instance.GetTileUVs(tile)); 41 } 42} 43

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Click4 : MonoBehaviour { 6 7 public static Click4 instance; 8 9 Dictionary<string, Vector2[]> tileUVMap; 10 11 public Sprite[] a; 12 13 // Use this for initialization 14 void Awake() 15 { 16 17 instance = this; 18 19 tileUVMap = new Dictionary<string, Vector2[]>(); 20 21 //Sprite[] sprites = Resources.LoadAll<Sprite> (""); 22 23 float imageWidth = 0f; 24 float imageHeight = 0f; 25 26 foreach (Sprite s in a) 27 { 28 29 if (s.rect.x + s.rect.width > imageWidth) 30 imageWidth = s.rect.x + s.rect.width; 31 32 if (s.rect.y + s.rect.height > imageHeight) 33 imageHeight = s.rect.y + s.rect.height; 34 } 35 36 foreach (Sprite s in a) 37 { 38 39 Vector2[] uvs = new Vector2[4]; 40 41 uvs[0] = new Vector2(s.rect.x / imageWidth, s.rect.y / imageHeight); 42 uvs[1] = new Vector2((s.rect.x + s.rect.width) / imageWidth, s.rect.y / imageHeight); 43 uvs[2] = new Vector2(s.rect.x / imageWidth, (s.rect.y + s.rect.height) / imageHeight); 44 uvs[3] = new Vector2((s.rect.x + s.rect.width) / imageWidth, (s.rect.y + s.rect.height) / imageHeight); 45 46 tileUVMap.Add(s.name, uvs); 47 } 48 49 } 50 51 // Update is called once per frame 52 void Update() 53 { 54 55 } 56 57 public Vector2[] GetTileUVs(Click1 tile) 58 { 59 60 string key = tile.type.ToString(); 61 62 if (tileUVMap.ContainsKey(key) == true) 63 { 64 65 return tileUVMap[key]; 66 } 67 else 68 { 69 70 Debug.LogError("There is no UV map for tile type: " + key); 71 return tileUVMap["Void"]; 72 } 73 } 74 75} 76

クリックした場所にメッシュを一つ生成するスクリプトを書いているのですが、二つ目のスクリプトの

tiles[i, j] = new Click1(Click1.Type.Water);

という箇所でエラーになります。

IndexOutOfRangeException: Array index is out of range.
Click2.Update () (at Assets/Scripts/Click2.cs:34)

どうしてエラーになるのか分からないので、教えてもらえないでしょうか?

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

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

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

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

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

guest

回答1

0

ベストアンサー

IndexOutOfRangeException は配列の範囲外の値を参照した時に発生するエラーです。この場合はtiles[,]の範囲を超えてi,jが指定されているため発生します。

配列の大きさは初期化時に決定します。tiles[,]の初期化はClick2のStart()で行われていますね。
tiles = new Click1[i, j];
ではこの時のi,jがいくつになっているかというと、両方とも0です。サイズ0の配列なので、どう参照しようと0な訳です。質問者さんがどのような動作を期待しているのかいまいちコードから汲み取れませんが、少なくともこのエラーの原因はここにあると思います。

投稿2018/07/26 23:19

ruccho

総合スコア285

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

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

退会済みユーザー

退会済みユーザー

2018/07/26 23:39

void Update () { if (Input.GetMouseButtonDown(0)) { Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); i = (int)pos.x; j = (int)pos.y; tiles = new Click1[i, j]; tiles[i, j] = new Click1(Click1.Type.Water); GenerateMesh(i, j); } } としたところ、 OverflowException: Number overflow. Click2.Update () (at Assets/Scripts/Click2.cs:33) と出ました。 iとjは0ではないと思うのですが、なぜこのエラーが出るのかよく分かりません
ruccho

2018/07/27 23:48

tiles = new Click1[i, j]; まずこれは、「tilesをi x jのサイズの2次元配列で初期化」を示します。 対して次の式は「tilesのインデックスi,jに新しいインスタンスを代入」です。 tiles[i, j] = new Click1(Click1.Type.Water); インデックスは0から開始するので、インデックスの最大値はi,jから1引いた数になります。よって、インデックスにi,jを指定すると範囲外となりエラーが発生します。
退会済みユーザー

退会済みユーザー

2018/07/28 02:32

回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問