• 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
1
Question by GamezAtWork · May 13, 2012 at 08:56 AM · c#editorarray

An Array of Classes with variables in C#

Well... I'm not that familiar with C# in Unity... So I'm a bit confused by this...

In Javascript, you can create a class, give it some public variables, and then make a public array with the class, so that you can have an array of classes to modify in the editor. Like...

 class thePerson{
 var theName:String;
 var theAge:String;
 }

Then after that, in another script, you have

 var thePeople:thePerson[];

Then in the editor, it will have a list of thePerson classes with theName and theAge of each thePerson modifiable...

How do you do this in C#?

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

2 Replies

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

Answer by syclamoth · May 13, 2012 at 09:17 AM

The issue here is that in UnityScript, custom classes are declared serializable implicity, wheras in C# you need to specify it using the 'System.Serializable' attribute:

 [System.Serializable]
 class ThePerson {
     // theVariables
 }

Then, so long as fields are declared public, you should have no problems.

 public ThePerson[] thePeople;
Comment
Add comment · Show 2 · 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 GamezAtWork · May 14, 2012 at 02:29 PM 0
Share

Thanks alot! It worked! :D

avatar image atifmahmood29 · Nov 22, 2014 at 02:28 PM 0
Share

Problem Solved.

avatar image
2

Answer by rutter · May 13, 2012 at 09:03 AM

Unity will expose any "public" value to the inspector. In UnityScript, variables are public unless you specify that they are private; in C#, variables are private unless you specify that they are public.

 //UnityScript
 var foo : int;         //seen in inspector
 private var foo : int; //NOT seen in inspector

 //C#
 public int foo; //seen in inspector
 int foo;        //NOT seen in inspector

If you want to learn more, Unity's script reference has some pretty detailed information at the SerializeField attribute page.

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 GamezAtWork · May 14, 2012 at 02:30 PM 0
Share

Thanks for the help! The SerializeField thing worked! And I much appreciate that little lesson on the differences between UnityScript and C# :)

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer

Need a hand using array.Length in a C# editor script 1 Answer

How to display a list of methods and allow a choice of one of them 1 Answer

Can a GameObject inside of an array be made "accessible" from the editor? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges