- Home /
How to expose script properties in C# ?
The documentation seems to contradict.
First it says that all behaviour scripts must derive from MonoBehaviour. Then it says that in order to display variables in the inspector, "you need create a class that derives from System.Object". How is it possible to do both? Am I misunderstanding what a "behaviour script" is? I thought it was a script that you attach to a game object.
Here is the first one: http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html
Here is the second one: http://unity3d.com/support/documentation/ScriptReference/Serializable.html
Answer by Ashkan_gc · May 13, 2011 at 07:21 AM
everything is derived from System.Object so there is no problem outthere. MonoBehaviours are derived from System.Object too.
yes a Behaviour is a script that can be attached to a gameObject and become enabled/disabled and receive events and ...
just declare your variables public and derive from MonoBehaviour then the variables will be exposed. some types of variables don't have default inspectors so without writing editor scripts you can not show them. ulong for example can not be exposed. also keep in mind that properties like public int something {get;set} can not be exposed (pure variables only).
Ok, I'll try this again. I'm in the middle of converting my whole project from JS to C#, and the public properties didn't appear to expose property pickers, but maybe that's because there were still compiler errors. Dunno.
I have finished the conversion and gotten rid of compiler errors, and now the public variables work with inspectors. That was the problem causing the confusion. Though I still think the documentation I mentioned above is confusing.
ok! i am happy that you ended up successfully! yes because unity uses reflection to get public properties from your script the script should be compiled for inspector to work. report a bug for the documentation part you don't like if you want.
Answer by Nicolaj Schweitz · May 13, 2011 at 07:27 AM
Hi,
Not 100% sure I understand your question, but [SerializeField] seems to be what you're looking for:
using UnityEngine; using System.Collections;
public class NewScript : MonoBehaviour {
[SerializeField] private NewScript myNewScript = new NewScript(); }
Answer by Meltdown · May 13, 2011 at 08:55 AM
Simply make your variables public like so...
using UnityEngine; using System.Collections;
public class NewScript : MonoBehaviour { public GameObject myGameObject; }
On your script in the inspector then you can drag the relevant gameObject to populate the variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133928im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Edit script properties under tabbed mode? 1 Answer
Best practice for properties "get{} set{}" ? 2 Answers
One script for all variables? 0 Answers