In one script:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class LevelGenerator : MonoBehaviour
{
public GameObject Node;
public Vector3 nodeScale = new Vector3(4, 1, 4);
[Range(3, 100)]
public int mapSize = 10;
public float Spacing = 10;
private List<GameObject> Nodes = new List<GameObject>();
private List<GameObject> EndStartPoints = new List<GameObject>();
private int currentMapSize;
private void Start()
{
currentMapSize = mapSize;
GenerateNew();
GenerateRandom(mapSize);
}
public void Generate()
{
if (Nodes.Count > 0)
Nodes = new List<GameObject>();
if (Node == null)
Node = UnityEngine.GameObject.FindGameObjectWithTag("Main Node");
for (int x = 0; x < mapSize; x++)
{
float positionWidth = Spacing * (float)x;
for (int z = 0; z < mapSize; z++)
{
float positionHieght = Spacing * (float)z;
GameObject Node = Instantiate(this.Node, Vector3.zero, this.Node.transform.rotation) as GameObject;
Node.transform.parent = transform;
Node.transform.localPosition = new Vector3(positionWidth, 0, positionHieght);
Node.transform.localScale = new Vector3(nodeScale.x, nodeScale.y, nodeScale.z);
Node.tag = "Node";
Node.name = "Node";
Nodes.Add(Node);
}
}
}
public void GenerateNew()
{
currentMapSize = mapSize;
DestroyNodes();
Node = UnityEngine.GameObject.FindGameObjectWithTag("Main Node");
Generate();
GenerateRandom(mapSize);
}
public void DestroyNodes()
{
GameObject[] nodes = UnityEngine.GameObject.FindGameObjectsWithTag("Node");
if (nodes.Length == 0)
{
//Debug.Log("No nodes to destroy found");
}
else
{
for (int i = 0; i < nodes.Length; i++)
{
DestroyImmediate(nodes[i]);
}
}
}
private void Update()
{
Erase();
}
private void Erase()
{
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Node")
{
Destroy(hit.collider.gameObject);
}
}
}
}
private void GenerateRandom(int gridSize)
{
List<int> selectedIndexes = new List<int>();
//Create an array for indexes of the TOP side
var arr = new int[gridSize];
for (int i = 0; i < gridSize; i++)
{
arr[i] = i;
}
int mynum = arr[Random.Range(0, arr.Length)];
GameObject Top = Nodes[mynum];
selectedIndexes.Add(mynum);
//Create an array for indexes of the LEFT side
int mynum1 = 0;
var arr1 = new int[gridSize];
for (int i = 0; i < gridSize; i++)
{
arr1[i] = i * gridSize;
}
do
{
mynum1 = arr1[Random.Range(0, arr1.Length)];
}
while (selectedIndexes.Contains(mynum1));
GameObject Left = Nodes[mynum1];
selectedIndexes.Add(mynum1);
//Create an array for indexes of the BOTTOM side
int mynum2 = 0;
var arr2 = new int[gridSize];
for (int i = 0; i < gridSize; i++)
{
arr2[i] = (gridSize * (gridSize - 1)) + i; //Reach the last row and iterate with i.
}
do
{
mynum2 = arr2[Random.Range(0, arr2.Length)];
}
while (selectedIndexes.Contains(mynum2));
GameObject Bottom = Nodes[mynum2];
selectedIndexes.Add(mynum2);
//Create an array for indexes of the RIGHT side
int mynum3 = 0;
var arr3 = new int[gridSize];
for (int i = 0; i < gridSize; i++)
{
arr3[i] = (i * gridSize) + gridSize - 1;
}
do
{
mynum3 = arr3[Random.Range(0, arr3.Length)];
}
while (selectedIndexes.Contains(mynum3));
GameObject Right = Nodes[mynum3];
selectedIndexes.Add(mynum3);
/*DestroyImmediate(Right);
DestroyImmediate(Left);
DestroyImmediate(Top);
DestroyImmediate(Bottom);*/
EndStartPoints = new List<GameObject>();
EndStartPoints.Add(Left);
EndStartPoints.Add(Right);
selectedIndexes.Clear();
}
private void OnValidate()
{
if (currentMapSize != mapSize)
{
UnityEditor.EditorApplication.delayCall += () =>
{
GenerateNew();
};
}
}
}
Then i'm creating a grid of gameobjects and it will be always 10x10 if it was 11 then 11x11 or 9x9.
In another script:
using UnityEngine.UI;
using System;
public class WayPoints : MonoBehaviour
{
public GameObject[] wayPoints;
[Range(5, 50)]
public int waypointsCount = 44;
private System.Random random = new System.Random();
// Use this for initialization
void Start ()
{
var Nodes = UnityEngine.GameObject.FindGameObjectsWithTag("Node");
}
// Update is called once per frame
void Update ()
{
}
}
I set the Range min to 5 and max to 50 But i know that the map size is 10 so there will be 100 gameobjects. So the range should be min 5 max 90 or maybe min 5 and max 50.
The problem is how can i make sure i will not set the Range max to be more then the map size ? If the map size will be 5 so it's 25 gameobjects i can't set the Range max to 50.
mapSize
variable inside. \$\endgroup\$