• 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
Question by Gillissie · May 13, 2011 at 07:09 AM · propertiesexpose

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.

Comment
Nicolaj Schweitz

People who like this

1 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 Nicolaj Schweitz · May 13, 2011 at 07:14 AM 1
Share

which documentation pages are you referring to?

avatar image Gillissie · May 13, 2011 at 08:00 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Ashkan_gc · May 13, 2011 at 07:21 AM

everyt$$anonymous$$ng 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 somet$$anonymous$$ng {get;set} can not be exposed (pure variables only).

Comment
loopyllama
Jean-Fabre
Nicolaj Schweitz
Gillissie

People who like this

3 Show 3 · 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 Gillissie · May 13, 2011 at 08:01 PM 0
Share

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.

avatar image Gillissie · May 14, 2011 at 02:27 AM 0
Share

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.

avatar image Ashkan_gc · May 14, 2011 at 02:47 AM 0
Share

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.

avatar image

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

Comment
loopyllama

People who like this

1 Show 0 · 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

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.

Comment
Nicolaj Schweitz

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Best practice for properties "get{} set{}" ? 2 Answers

How to hide script-completed shader properties ? 0 Answers

Instantiated object script properties 1 Answer

exposed properties ignore prefab apply 0 Answers

public properties don't update 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