Search Unity

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

Discussion in 'Scripting' started by Chocolade, Aug 20, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    In one script i have the variable:

    Code (csharp):
    1.  
    2. public int mapSize = 10;
    3.  
    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:

    Code (csharp):
    1.  
    2. using UnityEngine.UI;
    3. using System;
    4.  
    5. public class WayPoints : MonoBehaviour
    6. {
    7.     public GameObject[] wayPoints;
    8.  
    9.     [Range(5, 50)]
    10.     public int waypointsCount = 44;
    11.  
    12.     private System.Random random = new System.Random();
    13.  
    14.     // Use this for initialization
    15.     void Start ()
    16.     {
    17.         var Nodes = UnityEngine.GameObject.FindGameObjectsWithTag("Node");
    18.  
    19.        
    20.        
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update ()
    25.     {
    26.        
    27.     }
    28. }
    29.  
    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.
     
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Just set the highest part of the range to Mapsize * Mapsize.

    Also, I think Unity's Random.Range is slightly easier to use.
     
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    But the variable mapSize is in another script. It's not in the WayPoints script.
     
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Then do:
    Code (CSharp):
    1. public WayPoints WayPointScript;
    2.  
    3. //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.
     
    Chocolade likes this.