前提・実現したいこと
今現在UnityでRayを飛ばし、当たったオブジェクトの名前をstring型の配列に格納しているのですが、うまくできません。
格納する際にはひとつ条件があり、配列の中身に同じ名前がある場合は格納しないという風にコーディングしています。
発生している問題・エラーメッセージ
Element0 obj1
Element 1 obj2
・
・
・
こんな感じになっているのが予想でした。
### 該当のソースコード C#です [SerializeField] private string[] objName; //HITしたゲームオブジェクトの名前を格納 #endregion #region Event // Use this for initialization void Start() { CursorImage.enabled = false; rayFlag = false; objName = new string[5]; flag = false; } // Update is called once per frame void Update() { CursorImage.transform.position = Input.mousePosition; if (Input.GetMouseButton(0)) { CursorImage.enabled = true; rayFlag = true; } else { CursorImage.enabled = false; rayFlag = false; } if (rayFlag) { _ray = camera.ScreenPointToRay(Input.mousePosition); } //Rayが当たったオブジェクトの情報を入れる箱 RaycastHit hit; if (Physics.Raycast(_ray, out hit)) { if (hit.collider.tag == "cube") { for (int i = 0; i < 5; i ++) { if (objName[i] == null) { if (objName[i] != hit.collider.name) { objName[i] = hit.collider.gameObject.name; break; } } else { break; } } } } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/09 09:40