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 MegadanXzero · Nov 08, 2013 at 02:39 PM · privategame editor

Is it possible to make a C# script variable changeable in the Editor, without being public?

I have some C# scripts with a bunch of variables that I'd like to be able to change in the editor, to make testing and iterating easier, however I don't really want to have any of those variables be public since they really shouldn't be changed by anything within the game itself at runtime.

Though this is generic to a lot of scripts in my game, as an example in my Weapon script I have some variables like reload time, damage, firing speed etc... Now obviously nothing else should be able to ever change those variables, but I'd like to be able to initially set them from the editor so that I can control everything from within Unity itself rather than having to go out to various XML files or whatever to change things.

Is there any good way I can do this? My temporary solution was to have a public and private version of each variable. The public one gets set in the editor, and then on object creation the value is copied to the private version, and the public version is then ignored. That feels pretty awful to me though, doubling the amount of variables in each script and then never even using half of them after the first second.

Is there some proper way to do this, or should I just give in and load in values like this from XML files instead? That certainly feels cleaner, but I'd really like to have everything in the Editor itself.

Comment
Add comment
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

1 Reply

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

Answer by Loius · Nov 08, 2013 at 03:19 PM

You can have a custom editor call in to a function in the class itself:

 [Thing.cs]
 using UnityEngine;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 public class Thing : MonoBehaviour {
   [SerializeField] private int count;
 
 #if UNITY_EDITOR
   public void ShowEditGUI() {
     count = EditorGUILayout.IntField("Count",count);
   }
 #endif
 }
 
 ...
 [ThingInspector.cs]
 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(Thing))]
 public class ThingInspector : Editor {
   public override void OnInspectorGUI() {
     (target as Thing).ShowEditGUI();
   }
 }
Comment
Add comment · Show 4 · 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 MegadanXzero · Nov 08, 2013 at 03:37 PM 0
Share

That sounds perfect, thank you!

One thing I'm wondering though, since I don't see anything like a ListField even though the editor does support them, would a list require a for loop or something to show each entry in the list manually? Something like:

 public void ShowEditGUI()
 {
     foreach (ListItem item in variableList)
     {
         item = EditorGUILayout.IntField("Item", item);
     }
 }

Is that sensible? Or is there something that does that sort of thing automatically that I'm missing?

avatar image Loius · Nov 08, 2013 at 05:19 PM 0
Share

As far as I know there isn't a simple thing that automatically does that. (you'd have to use a for loop though; foreach variables can't be altered)

avatar image MegadanXzero · Nov 08, 2013 at 05:26 PM 0
Share

Hmm, I've just tried implementing this and it seems that simply putting [SerializeField] in front of a List variable, without creating an Inspector class or making a ShowEditGUI function at all works automatically.

I guess that means the latter part is only necessary if you're using custom structs/classes within your class that Unity won't know how to display otherwise?

avatar image Loius · Nov 08, 2013 at 09:10 PM 0
Share

I haven't not made a custom editor for a class in forever!

You're right, just SerializeField is enough if there's no custom editor at all.

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

16 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

Related Questions

RTS Style Selection system (How to control one unit and not the others) 2 Answers

Help with making a script in the unity editor 2 Answers

Private variable losing its value on editor script 1 Answer

Variables not visible in editor but accessible from other scripts 2 Answers

Always play music? 1 Answer


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