How can i set a Range values min/max according to another variable value ?

In one script i have the variable:

public int mapSize = 10;

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 ans 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.

Just set the highest part of the range to Mapsize * Mapsize.

Also, I think Unity’s Random.Range is slightly easier to use.

But the variable mapSize is in another script. It’s not in the WayPoints script.

Then do:

public WayPoints WayPointScript;

//WayPointScript.mapSize;

Don’t forget to drag the object with the WayPoints script into the Inspector slot that will show when you declare the public class.

1 Like