Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

index was out of range ( Random spawn positions ) ,Index was out of range

$
0
0
**Hi, so i'm doing simple cube game that needs random cube spawn position but i got error "index wwas out of range" plz help :) ** using UnityEngine; using System; using System.Collections; using System.Collections.Generic; public class GameController : MonoBehaviour { private CubePos nowCube = new CubePos(0, 1, 0); public float cubeChangePlaceSpeed = 0.5f; public Transform cubeToPlace; public GameObject cubeToCreate, allCubes; private List allCubesPositions = new List { new Vector3(0,0,0), new Vector3(1,0,0), new Vector3(-1,0,0), new Vector3(0,1,0), new Vector3(0,0,1), new Vector3(0,0,-1), new Vector3(1,0,1), new Vector3(-1,0,-1), new Vector3(-1,0,1), new Vector3(1,0,-1), }; private void Start() { StartCoroutine(ShowCubePlace()); } private void Update() { if (Input.GetMouseButtonDown(0) || Input.touchCount > 0) { GameObject newCube = Instantiate(cubeToCreate, cubeToPlace.position, Quaternion.identity) as GameObject; newCube.transform.SetParent(allCubes.transform); nowCube.setVector(cubeToPlace.position); allCubesPositions.Add(nowCube.getVector()); SpawnPositions(); } } IEnumerator ShowCubePlace() { while (true) { SpawnPositions(); yield return new WaitForSeconds(cubeChangePlaceSpeed); } } private void SpawnPositions() { List positions = new List(); if (IsPositionEmpty(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z))&& nowCube.x + 1 != cubeToPlace.position.x) { positions.Add(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z)); } if (IsPositionEmpty(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z))&& nowCube.x - 1 != cubeToPlace.position.x) { positions.Add(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z)); } if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z))&& nowCube.y + 1 != cubeToPlace.position.y) { positions.Add(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z)); } if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z))&& nowCube.y - 1 != cubeToPlace.position.y) { positions.Add(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z)); } if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1))&& nowCube.z + 1 != cubeToPlace.position.z) { positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1)); } if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1))&& nowCube.z - 1 != cubeToPlace.position.z) { positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1)); } cubeToPlace.position = positions[UnityEngine.Random.Range(0, positions.Count)]; } private bool IsPositionEmpty(Vector3 targetPos) { if (targetPos.y == 0) { return false; } foreach (Vector3 pos in allCubesPositions) { if (pos.x == targetPos.x && pos.y == targetPos.y && pos.z == targetPos.z) { return false; } } return true; } } struct CubePos { public int x, y, z; public CubePos(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public Vector3 getVector() { return new Vector3(x, y, z); } public void setVector(Vector3 pos) { x = Convert.ToInt32(pos.x); y = Convert.ToInt32(pos.x); z = Convert.ToInt32(pos.x); } } , **Why i'm getting error out of range? i'm doing simple block game that creates random positions.** using UnityEngine; using System; using System.Collections; using System.Collections.Generic; public class GameController : MonoBehaviour { private CubePos nowCube = new CubePos(0, 1, 0); public float cubeChangePlaceSpeed = 0.5f; public Transform cubeToPlace; public GameObject cubeToCreate, allCubes; private List allCubesPositions = new List { new Vector3(0,0,0), new Vector3(1,0,0), new Vector3(-1,0,0), new Vector3(0,1,0), new Vector3(0,0,1), new Vector3(0,0,-1), new Vector3(1,0,1), new Vector3(-1,0,-1), new Vector3(-1,0,1), new Vector3(1,0,-1), }; private void Start() { StartCoroutine(ShowCubePlace()); } private void Update() { if (Input.GetMouseButtonDown(0) || Input.touchCount > 0) { GameObject newCube = Instantiate(cubeToCreate, cubeToPlace.position, Quaternion.identity) as GameObject; newCube.transform.SetParent(allCubes.transform); nowCube.setVector(cubeToPlace.position); allCubesPositions.Add(nowCube.getVector()); SpawnPositions(); } } IEnumerator ShowCubePlace() { while (true) { SpawnPositions(); yield return new WaitForSeconds(cubeChangePlaceSpeed); } } private void SpawnPositions() { List positions = new List(); if (IsPositionEmpty(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z))&& nowCube.x + 1 != cubeToPlace.position.x) { positions.Add(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z)); } if (IsPositionEmpty(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z))&& nowCube.x - 1 != cubeToPlace.position.x) { positions.Add(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z)); } if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z))&& nowCube.y + 1 != cubeToPlace.position.y) { positions.Add(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z)); } if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z))&& nowCube.y - 1 != cubeToPlace.position.y) { positions.Add(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z)); } if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1))&& nowCube.z + 1 != cubeToPlace.position.z) { positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1)); } if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1))&& nowCube.z - 1 != cubeToPlace.position.z) { positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1)); } cubeToPlace.position = positions[UnityEngine.Random.Range(0, positions.Count)]; } private bool IsPositionEmpty(Vector3 targetPos) { if (targetPos.y == 0) { return false; } foreach (Vector3 pos in allCubesPositions) { if (pos.x == targetPos.x && pos.y == targetPos.y && pos.z == targetPos.z) { return false; } } return true; } } struct CubePos { public int x, y, z; public CubePos(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public Vector3 getVector() { return new Vector3(x, y, z); } public void setVector(Vector3 pos) { x = Convert.ToInt32(pos.x); y = Convert.ToInt32(pos.x); z = Convert.ToInt32(pos.x); } }

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>