Sorting an Array of Arrays for game Object
The purpose of the code is to check the spawn point of the unit being spawned, and if there is a game object there, to not spawn an object, but I have hit massive amounts of hurdles in what should have been a simple task. The new error here is that my multidimensional array is being temperamental
I have used Nested for-loops in an attempt to sort through a 2D array, but they keep giving me an error saying a nested loop initializer was expected, thing is when I switch out "[,]" for "[][]", I can no longer use the transform.position methods, so i need that "[,]" initializer, my code is included below, i will be most gratefully if you can repair this error and finally put an end to a code I've worked on for much to long
using UnityEngine; using System.Collections; using UnityEngine.UI; using Random = UnityEngine.Random;
public class Mspawn : MonoBehaviour { public Button button; public BoardManager boardManger;
public Vector3 Coordinate;
void OnEnable(){
if (Input.GetMouseButtonDown(0)) {
button.onClick.AddListener (SpawnUnit);
}
}
void Awake(){
boardManger = GetComponent<BoardManager> ();
}
void SpawnUnit()
{
Coordinate = randomSpawnPosition ();
for (int i = 0; i < boardManger.PlayerUnits.Length; i++)
{
if (boardManger.PlayerUnits[i].CompareTag("PlayerSoldierMarksman"))
{
if (checkposition(Coordinate) == false)
{
Instantiate (boardManger.PlayerUnits[i], new Vector3 (Coordinate.x,Coordinate.y,0f), Quaternion.identity);
}
Debug.Log("Selected Spawn Position "+ Coordinate);
}
}
}
public bool checkposition(Vector3 SpawnedPosition)
{
// This is the problem code, it returns a "Expected Nested array Initializer was expected" error
GameObject[] AllMountains = GameObject.FindGameObjectsWithTag("Mountain");
GameObject[] AllPlayerHQ = GameObject.FindGameObjectsWithTag ("PlayerHQ");
GameObject[] AllMarksman = GameObject.FindGameObjectsWithTag ("PlayerSoldierMarksman");
GameObject[,] AllActive = {AllMarksman, AllMountains, AllPlayerHQ};
int maxRow = AllActive.GetLength(0);
int maxCol = AllActive.GetLength (1);
for (int i =0; i <= maxRow; i++)
{
for (int x = 0; x<= maxCol; x++)
{
if (AllActive[i,x].transform.position == SpawnedPosition)
{
Debug.Log("Spawn Blocked");
return true;
}
}
}
return false;
}
Vector3 randomSpawnPosition()
{
int x = Random.Range(0,2);
int y = Random.Range(0,2);
Vector3 Position = new Vector3 (x, y,0f);
return Position;
}
}
Answer by M-Hanssen · May 18, 2016 at 02:44 PM
Change your method to the following:
public bool checkposition(Vector3 SpawnedPosition)
{
List<List<GameObject>> allActive = new List<List<GameObject>>();
allActive.Add(GameObject.FindGameObjectsWithTag("Mountain").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("PlayerHQ").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("PlayerSoldierMarksman").ToList());
if (allActive.Any(activeItem => activeItem.Any(activeGameObject => activeGameObject.transform.position == SpawnedPosition)))
{
Debug.Log("Spawn Blocked");
return true;
}
return false;
}
Make sure to add using System.Linq;
This code does not seem to function for some reason, it give a "List 1 could not be found, are you missing a directory ect....." error, and the code it'self is red, which from my experience indicates that i am unable to use it
List is a Class inside the namespace System.Collections.Generic.
$$anonymous$$ake sure to add using System.Linq;
and using System.Collections.Generic;
on top of your script!!!
You can make use of Resharper for Visual Studio to find the missing references for you.
This actually works now, thank you, and it also gets rid of that Array out of index problem i was having with the arrays, so you have done way more then asked, thankyou sir ^_^ have a wonderful evening
Answer by Brutalitywarlord · May 19, 2016 at 02:39 PM
I have found the error, instead of using [i,x] i switch it to [i][x] instead