- Home /
How can i limit int variables in the Inspector so the user will not be able to enter the numbers 2 or not same numbers ?
In the top of the script:
public int mapWidth = 10;
public int mapHeight = 10;
Then in a loop:
private void Generate()
{
if (objects.Count > 0)
objects = new List<GameObject>();
if (Node == null)
Node = GameObject.FindGameObjectWithTag("Main Node");
for (int x = 0; x < mapWidth; x++)
{
float positionWidth = Spacing * (float)x;
for (int z = 0; z < mapHeight; z++)
{
float positionWidth1 = Spacing * (float)z;
GameObject block = Instantiate(Node, Vector3.zero, Node.transform.rotation) as GameObject;
block.transform.parent = transform;
block.transform.localPosition = new Vector3(positionWidth, 0, positionWidth1);
block.transform.localScale = new Vector3(nodeScale.x, nodeScale.y, nodeScale.z);
block.tag = "NodeDestroyable";
objects.Add(block);
}
}
}
But i want to add two rules:
The user will not be able to enter as int numbers 2 or less. So mapWidth or/and mapHeight will never be 2 both or one of them and also the numbers in both int variables should be the same like 7 and 7 or 10 and 10 but not 8 and 9 or 67 and 4.
If the user enter this numbers prevent from him to generate. And i have a button that i click to Generate so i need also to take care the button maybe somehow to enable/disable the button: This is the button class:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Generator))]
public class GenerateButton : Editor
{
public override void OnInspectorGUI()
{
Generator Generate = (Generator)target;
if (GUILayout.Button("Generate New Map"))
{
Generate.GenerateNew();
}
DrawDefaultInspector();
if (GUILayout.Button("Destroy Nodes"))
{
Generate.DestroyNodes();
}
}
}
The idea is to prevent from the user to enter this values and also to prevent the user from clicking the button if the values are wrong.
About enable/disable the button i can't find any way to do it. I can disable the whole guilayout but it will disable also the other variables i want to enable/disable only the button.
Answer by ShadyProductions · Aug 14, 2017 at 10:23 PM
There is something on a MonoBehaviour called OnValidate() which allows you to check variables in the inspector and reset them etc for example:
public int NumberOfEnemies; //set in inspector
void OnValidate() {
if (NumberOfEnemies > 5)
NumberOfEnemies = 5; // Reset to 5 when it is tried to be set higher than 5
}
This is working in general but then when i put a wrong number in the Inspector for example if it''s > 5 and i put 7 i need to click with the mouse on something else if not it will not update the value.
It's like i need some refresh or invalidate inside the OnValidate. I could use [ExecuteInEdit$$anonymous$$ode] but i don't want to use it.
This can partly be solved by using the DelayedAttribute on your variable. This will instruct the editor to not update the value while editing it. So when you finish editing the value OnValidate will be triggered before the value is saved.
Of course the Range attribute may also be an alternative. However it will show a slider with the defined range.
Your answer

Follow this Question
Related Questions
How do i change the GUI.Box font size and box size ? 0 Answers
How can i first destroy twice the objects then to create twice the objects ? 1 Answer
How can i rotate a transform using lerp but keep the nose in same direction/angle on x ? 1 Answer
To pass a variable between scenes should I use scriptableobject or static ? 1 Answer
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers