0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Please insert the second component, this one with mapSize variable inside. \$\endgroup\$ – Jacob Aug 20 '17 at 20:13
  • \$\begingroup\$ @Spectre Insert where ? The variable mapSize is in another script not in the WayPoints script. \$\endgroup\$ – Dragon Flea Aug 20 '17 at 20:33
  • \$\begingroup\$ Insert that entire script here please! Edit the post :) Then, I'll help you out \$\endgroup\$ – Jacob Aug 20 '17 at 20:38
2
\$\begingroup\$

Usually I do it something like this:

[Range(0, 1)]
public float proportionToPick;

public int maxItems = 50;

int _numberToPick;

void Start() {
     _numberToPick = Mathf.FloorToInt(maxItems * proportionToPick);

     ...
}

This way you can tune proportionToPick to 0.2 if you want to grab about a fifth of the items in the collection, no matter if that collection is 50 items or 5 or 5000. It will automatically scale in proportion to your count.

If you need to be able to set the number of items more explicitly, you can do this with a Custom Editor, where you can manually control every detail of how the script looks & works in the Inspector. This is a fair bit of extra code though so I usually work around it with what I can do in a single script unless I need something complicated.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.