現在Unityで1人称視点のホラーゲーム制作を行っております。
そこでドアやバケツの蓋を開けるアニメーションを作り、Unityで実行したいのですが、クリックすると以下のようなエラーメッセージが出て困っておりますので、ご教授いただけましたら幸いです。
###発生しているエラーメッセージ
IndexOutOfRangeException: Array index is out of range. huta.PlayAnimation (UnityEngine.Animation anime) (at Assets/gamen/Assets/script/huta.cs:69) huta.Update () (at Assets/gamen/Assets/script/huta.cs:47)
###該当のソースコード
C#
1using UnityEngine; 2using System.Collections; 3 4public class huta : MonoBehaviour 5{ 6 public float distance = 3f; 7 private int hcounter = 0; 8 private int dcounter = 0; 9 public int x; 10 public int y; 11 public Camera camera; 12 GameObject bucket; 13 GameObject door; 14 public AnimationClip[] animClip; 15 public AnimationClip[] animeClip; 16 17 18 // Update is called once per frame 19 void Start() 20 { 21 camera = GameObject.Find ("MainCamera").gameObject.GetComponent<Camera>(); 22 bucket = GameObject.Find ("bucket").gameObject; 23 door = GameObject.Find("door").gameObject; 24 } 25 26 void Update() 27 { 28 if (Input.GetMouseButtonDown(0)) 29 { 30 Vector3 screen_point = Input.mousePosition; 31 screen_point.z = 2.0f; 32 //Debug.Log (camera.ScreenToWorldPoint(screen_point)); 33 Ray ray = camera.ScreenPointToRay(screen_point); 34 RaycastHit hit = new RaycastHit(); 35 36 //マウスクリックした場所からRayを飛ばし、オブジェクトがあればtrue 37 if (Physics.Raycast(ray, out hit, distance)) 38 { 39 Debug.Log("Raycast"); 40 if (hit.collider.tag == "huta") 41 { 42 Debug.Log ("tag判定"); 43 hcounter++; 44 Animation anime = bucket.GetComponent<Animation>(); 45 x = hcounter % 2; 46 Debug.Log (x); 47 PlayAnimation(anime); 48 } 49 else if (hit.collider.tag == "door") 50 { 51 dcounter++; 52 Animation anime2 = door.GetComponent<Animation>(); 53 y = dcounter % 2; 54 PlayAnimation(anime2); 55 } 56 } 57 } 58 } 59 60 void PlayAnimation(Animation anime) 61 { 62 if (x == 0) 63 { 64 anime.AddClip(animClip[0], "closehuta"); 65 anime.Play("closehuta"); 66 } 67 else if (x == 1) 68 { 69 anime.AddClip(animClip[1], "huta"); 70 anime.Play("huta"); 71 } 72 } 73 74 void PlayAnimetion(Animation anime2) 75 { 76 if(y == 0) 77 { 78 anime2.AddClip(animeClip[0], "closedoor"); 79 anime2.Play("closedoor"); 80 } 81 else if(y == 1) 82 { 83 anime2.AddClip(animeClip[1], "door"); 84 anime2.Play("door"); 85 } 86 } 87 88}
###補足情報(言語/FW/ツール等のバージョンなど)
言語:C#
Unityのバージョン:5.5.0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/02 05:02