実現したいこと
IndexOutOfRangeException: Index was outside the bounds of the array.
Sample.Start () の解決をしたい
前提
特定のタグの中から2つを選んで別タグにしようとしたときにIndexOutOfRangeException: Index was outside the bounds of the array.
Sample.Start () が出た。
発生している問題・エラーメッセージ
IndexOutOfRangeException: Index was outside the bounds of the array. Sample.Start () (at Assets/Sample.cs:38)
該当のソースコード
unity
1using Photon.Pun; 2using UnityEngine; 3using System; 4using System.Linq; 5using System.Collections; 6using System.Collections.Generic; 7 8public class Sample : MonoBehaviourPunCallbacks 9{ 10 public GameObject camera; 11 public int desiredNumberCount = 2; 12 public string oldTag; 13 public string newTag; 14 15 private void Start() 16 { 17 PhotonNetwork.IsMessageQueueRunning = true; 18 19 PhotonNetwork.NickName = "Player"; 20 PhotonNetwork.ConnectUsingSettings(); 21 var position = new Vector3(UnityEngine.Random.Range(-3f, 3f), UnityEngine.Random.Range(-3f, 3f)); 22 GameObject avatar = PhotonNetwork.Instantiate("Avatar", position, Quaternion.identity); 23 24 25 int num = PhotonNetwork.CurrentRoom.PlayerCount; 26 camera.transform.parent = avatar.transform; 27 28 if (PhotonNetwork.IsMasterClient) 29 { 30 PhotonNetwork.CurrentRoom.SetStartTime(PhotonNetwork.ServerTimestamp); 31 } 32 33 GameObject[] objects = GameObject.FindGameObjectsWithTag(oldTag); 34 int[] uniqueRandomNumbers = Enumerable.Range(0, objects.Length).OrderBy(x => Guid.NewGuid()).Take(desiredNumberCount).ToArray(); 35 36 for (int i = 0; i < desiredNumberCount; i++) 37 { 38 objects[uniqueRandomNumbers[i]].tag = newTag; 39 } 40 } 41}

あなたの回答
tips
プレビュー