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
2
Question by Gintas_ · Apr 21, 2013 at 10:28 AM · c#inspectorpublic variableread-onlyrun-time

How to make public variable read-only during run-time?

I have a simple variable:

 public enum something { one, two }
 public something variable = something.one;

And I want to don't let user to change that variable in the inspector during run-time. I need to somehow make it read-only. Any ideas?

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 freshingrain · Jul 29, 2016 at 11:57 PM 0
Share

We use this plugin to make public variable read-only in the inspector. It provides a [NotEditableInInspector] attribute.

avatar image tanoshimi freshingrain · Jul 30, 2016 at 08:06 AM 0
Share

The OP asked to make public variables read-only during runtime, as answered by @$$anonymous$$emLeak. That asset just makes public variables read-only in the inspector, which is easily achieved using a propertyattribute.

3 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by rsodre · Jan 14, 2017 at 03:18 PM

I adapted the scripts from this article to achieve this.

ReadOnlyWhenPlayingAttribute.cs:

 using UnityEngine;
 public class ReadOnlyWhenPlayingAttribute : PropertyAttribute { }

Editor/ReadOnlyWhenPlayingAttributeDrawer.cs

 using UnityEngine;
 using UnityEditor;
 
 [CustomPropertyDrawer(typeof(ReadOnlyWhenPlayingAttribute))]
 public class ReadOnlyWhenPlayingAttributeDrawer : PropertyDrawer
 {
     // Necessary since some properties tend to collapse smaller than their content
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return EditorGUI.GetPropertyHeight(property, label, true);
     }
 
     // Draw a disabled property field
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         GUI.enabled = !Application.isPlaying;
         EditorGUI.PropertyField(position, property, label, true);
         GUI.enabled = true;
     }
 }

Usage:

 [ReadOnlyWhenPlaying]
 public something variable = something.one;

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 cecarlsen · Mar 02, 2018 at 12:30 PM 0
Share

Brilliant! This simplifies a lot of my scripts.

avatar image
5

Answer by whydoidoit · Apr 21, 2013 at 10:29 AM

You need to make the field private and add [SerializeField] then expose it as a read only property:

   [SerializeField]
   private something _variable;

   public something variable { get { return _variable; } }
Comment
Add comment · Show 7 · 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 Gintas_ · Apr 21, 2013 at 10:53 AM 0
Share

Tried it right now, variable still can be changed during run-time, so it doesn't work.

avatar image whydoidoit · Apr 21, 2013 at 11:00 AM 0
Share

Oh I've got what you want now - that is not possible - I'm struggling to think of why you are worrying about changing it in the inspector at run time? Why would you want to do that??

avatar image whydoidoit · Apr 21, 2013 at 11:02 AM 1
Share

The code I gave you stops the variable being changed in code from another script.

avatar image Gintas_ · Apr 21, 2013 at 11:14 AM 0
Share

I need it because if user changes character movement type during run-time, the whole movement system doesn't work anymore. So there isn't better solution than creating another variable, changing it's value when script starts and then in Update() setting the original variable to the new? Would look like this

avatar image whydoidoit · Apr 21, 2013 at 11:16 AM 0
Share

But why would the user be messing around in the inspector? At runtime? Well fair enough - yes - you have to have a private variable initialized from a public variable during Start or Awake.

Show more comments
avatar image
2

Answer by Memleak · Jul 13, 2014 at 10:00 PM

For anyone still interested on how to achieve this behavior do as follow:

 [SerializeField]
 private <Type> InternalVariable = default;
 
 //To be accessed from this class or other scripts.
 public <Type> Variable { get {return InternalVariable;}; }
 
 void Update()
 {
     UpdateInternals();
 }
 
 void UpdateInternals()
 {
     if (InternalVariable != Variable)
     {
         Debug.Log("InternalVariable can only be set at compile time. Your change will not be applied.");
         InternalVariable = Variable;
     }
     //Process any other internals
 }

What actually happens is that whatever is set at startup in InternalVariable is then used while Unity runs. You can even make it such as just in certain circumstances this will be disabled.

I like to use that variable as private and use "Internal" in its name so its purpose is clear. If I want to handle it from scripting I use its property instead (create a setter for it, of course).

Not the best pattern but the nice thing is you can use C# partial classes to remove the Inspector behavior from the actual class. Having a classInspector.cs will clearly indicate what you will find there.

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 MrFuzzzy · Jul 23, 2020 at 01:54 PM 0
Share

nice, but would be cool to have a simpler way to do that.

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

17 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

Related Questions

C# | public variables in parent class should not show up in child class 1 Answer

C# public decimal variable not showing up in inspector 2 Answers

Multiple Cars not working 1 Answer

c# public variables not appearing in inspector 0 Answers

Distribute terrain in zones 3 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