Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FederalRazer89 · Nov 07, 2020 at 05:02 PM · scripting problemeditor-scriptingvariablesmonobehaviour

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Nov 10, 2020 at 06:34 PM 0
Share

Why not have getter functions that return the variables?

avatar image FederalRazer89 $$anonymous$$ · Nov 18, 2020 at 04:13 PM 0
Share

I am fairly new to C# so i am not really sure how i would apply a getter function to my code yet.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
             
         }
 
         
     }
     
 
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FederalRazer89 · Nov 18, 2020 at 04:07 PM 0
Share

TerrainVariblesAssembly just store multiple variables in one variable

avatar image
0

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]

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FederalRazer89 · Nov 18, 2020 at 04:06 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

263 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges