- Home /
Trying to get access to variables in the editor script
I am trying to get my script to run in the editor when i press a button, but i have 4 variables which which is not public so i don't have access to them in editor script. These 4 variables are checked by the update function, and they are used by the TerrainManagerStart function. I am new to editor scripting and a semi noob in C#
The affected varibles:
CustomTerrainValues customTerrainValues;
TerrainThreadContainer terrainThreadContainer;
ConcurrentQueue<TileData> tileCQ = new ConcurrentQueue<TileData>();
Vector3[] tilePos;
public class TerrainManager : MonoBehaviour
{
//the max tileMeshSize is close to 250
public int tileMeshSize = 250;
[Range(5, 1000)]
public int tileCount = 20;
public int continentMassModifier = 10;
public int continentDistanceMax = 250;
public int continentAmount = 1;
public int tileSizeScaleModifier = 1;
public int perlinNoiseScale = 1;
public int perlinNoiseHeigh = 1;
public int settelmentCount = 0;
public int cityCount = 0;
public int miningCount = 0;
bool terrainGenerationStarted = false;
//Add Biomes
//Terrain characteristics for diffrent areas examples hills ,canyon, plains, sanddunes.
CustomTerrainValues customTerrainValues;
TerrainThreadContainer terrainThreadContainer;
ConcurrentQueue<TileData> tileCQ = new ConcurrentQueue<TileData>();
Vector3[] continetArray;
Vector3[] tilePos;
void Start()
{
tilePos = new Vector3[tileCount * tileCount];
terrainThreadContainer = new TerrainThreadContainer(tileCount);
customTerrainValues = TerrainVariblesAssembly
(
tileMeshSize,
tileCount,
tileSizeScaleModifier,
continentMassModifier,
continentDistanceMax,
continentAmount,
perlinNoiseScale,perlinNoiseHeigh,
settelmentCount,
cityCount,
miningCount
);
TerrainManagerStart(tilePos, customTerrainValues, terrainThreadContainer, tileCQ);
}
Editor script
public class TerrainGeneratorEditor : Editor
{
public override void OnInspectorGUI()
{
TerrainManager terrainManager = (TerrainManager)target;
base.OnInspectorGUI();
if (GUILayout.Button("Generate world"))
{
terrainManager.TerrainVariblesAssembly(terrainManager.tileMeshSize,terrainManager.tileCount, terrainManager.tileSizeScaleModifier, terrainManager.continentMassModifier, terrainManager.continentDistanceMax, terrainManager.continentAmount, terrainManager.perlinNoiseScale, terrainManager.perlinNoiseHeigh,
terrainManager.settelmentCount, terrainManager.cityCount,terrainManager.miningCount);
terrainManager.TerrainManagerStart(tilePos, customTerrainValues, terrainThreadContainer, tileCQ);
}
}
}
I could solve this by using a while loop that would replace the update(). Do need to rethink how i approach the code? Do i need to try in bake these 4 variables in the function? preferable i don't want to make those variables public.
Why not have getter functions that return the variables?
I am fairly new to C# so i am not really sure how i would apply a getter function to my code yet.
Answer by FederalRazer89 · Nov 19, 2020 at 12:48 PM
I solved my problem on my own, by rewrite my scripts to not check thread tasks against the MonoBehaviour update() and instead use a while loop. These include 3 different C# files with around 150 lines of code so its too much to share, so that would became a wall of text. But the 4 variables have all been baked into TerrainManagerStart() and some other script.
Script
public int tileMeshSize = 250;
[Range(5, 1000)]
public int tileCount = 20;
public int continentMassModifier = 10;
public int continentDistanceMax = 250;
public int continentAmount = 1;
public int tileSizeScaleModifier = 1;
public int perlinNoiseScale = 1;
public int perlinNoiseHeigh = 1;
public int settelmentCount = 0;
public int cityCount = 0;
public int miningCount = 0;
void Start()
{
CustomTerrainValues customTerrainValues = TerrainVariblesAssembly ( tileMeshSize, tileCount, tileSizeScaleModifier, continentMassModifier, continentDistanceMax, continentAmount, perlinNoiseScale, perlinNoiseHeigh, settelmentCount, cityCount, miningCount );
TerrainManagerStart(customTerrainValues);
}
Editor
public class TerrainGeneratorEditor : Editor
{
public override void OnInspectorGUI()
{
TerrainManager terrainManager = (TerrainManager)target;
base.OnInspectorGUI();
if (GUILayout.Button("Generate world"))
{
CustomTerrainValues customTerrainValues = terrainManager.TerrainVariblesAssembly
(
terrainManager.tileMeshSize,
terrainManager.tileCount,
terrainManager.tileSizeScaleModifier,
terrainManager.continentMassModifier,
terrainManager.continentDistanceMax,
terrainManager.continentAmount,
terrainManager.perlinNoiseScale, terrainManager.perlinNoiseHeigh,
terrainManager.settelmentCount,
terrainManager.cityCount,
terrainManager.miningCount
);
terrainManager.TerrainManagerStart(customTerrainValues);
}
}
}
Answer by Bunny83 · Nov 10, 2020 at 08:30 PM
Why do you pass variables that are stored in the same class where the method is located as parameters? Why don't you just use the variables inside the method directly? That's the point of having instance methods. Instance methods work on the instance and have access to everything inside that instance.
Since we don't know what the TerrainVariblesAssembly actually looks like and what it does it's kinda pointless to speculate about that.
TerrainVariblesAssembly just store multiple variables in one variable
Answer by logicandchaos · Nov 11, 2020 at 04:01 AM
The whole point of using public variables is to access them from other scripts, if you want to access them from other scripts but don't want to make them public then you need public methods to get and set the variable, or use a property. If you want to access the variable from the editor without making it public then you have to add [SerializedField]
Just tested to make them public, to se if i could get it to run in editor. But i got a error, which i don't when i execute the script with the play button, and it runs. So i think i will try to remake the script to work in editor.
Answer by abbabon · Nov 18, 2020 at 04:30 PM
As others have commented, you have to expose something for it to be used externally.
I would encapsulate that logic inside a public instance method on the original class (public void GenerateWorld()), so I wouldn't have to expose 'sensitive' members outside.
Your answer
Follow this Question
Related Questions
Variables modified on other scripts through a Editor Script reset on Play? 1 Answer
Custom Inspector: Accessing a reference to another MonoBehaviour? 1 Answer
I'm unable to clear a variable that is used in multiple scripts. 0 Answers
Confused about custom GameObjects,Custom GameObject confusion 0 Answers
Access variables,method from a Mono Behaviour without static use. -1 Answers