Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by _Petroz · Dec 20, 2010 at 09:54 AM · arrayinspectorpublic

public fixed size array in inspector

Is there a way to create a public fixed size array visible in the inspector? I want to do something like the Particle Animator's 'Color Animation' variable where it appears like this in the inspector:

ColorAnimation[0]
ColorAnimation[1]
ColorAnimation[2]
ColorAnimation[3]
ColorAnimation[4]
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

4 Replies

· Add your reply
  • Sort: 
avatar image
15

Answer by v01pe_ · Sep 13, 2015 at 09:43 PM

I recently did this by using OnValidate() to check for size modifications and resetting the size to what I want it to be, while keeping the array contents:

 private const int SIZE = 5;
 public int[] ints = new int[SIZE];
 
 void OnValidate()
 {
     if (ints.Length != SIZE)
     {
         Debug.LogWarning("Don't change the 'ints' field's array size!");
         Array.Resize(ref ints, SIZE);
     }
 }

Only downside is, that when you have a second variable referencing the same array for whatever reason (like private int[] myInts = ints), the second variable still keeps the old array. But that's not something one usually does I guess.

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 usernameHed · Feb 05, 2018 at 03:48 PM 0
Share

And you even can set it to private if you set it to [SerializeField] ! Very nice

avatar image
3

Answer by Molix · Feb 07, 2011 at 03:17 PM

I think you'd need to use a custom inspector. In DrawInspectorGUI, you'd have something like:

void OnInspectorGUI() { MyComponent myComp = target as MyComponent;

for( int i=0; i<5; ++i ) { myComp.ColorAnimation[i] = EditorGUILayout.ColorField( "ColorAnimation[" + i + "]",
myComp.ColorAnimation[i] ); } }

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

Answer by Berenger · Dec 20, 2010 at 10:38 AM

if you mean to rofbid possibility for users to change the size, I'd say use regular variables. But if you just want your array to appear in the inspector, you need built-in array :

var ColorAnimation : Color[] = new Color[5];

Then, in the inspector, you'll have :

ColorAnimation :
Size 5
Element 0 = Color
Element 1 = Color
Element 2 = Color
Element 3 = Color
Element 4 = Color
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 _Petroz · Dec 21, 2010 at 03:08 AM 0
Share

The problem is the array can be resized from the inspector which is not what I want.

avatar image alisher8 _Petroz · Apr 25 at 06:46 AM 0
Share

how can we set the array size by using script? i mean i know we can set it manually in inspector but i am working on something and it's size must be changed by script, any one help me?

avatar image
0

Answer by alisher8 · Apr 25 at 06:47 AM

Hey everyone i know this is a very old Q/A but can someone help me in how can we set the array size by using script? i mean i know we can set it manually in inspector but i am working on something and it's size must be changed by script, any one help me?

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 Augustolego55 · May 25 at 02:12 PM 0
Share

For an array, you need to redefine it:

For an int array: int array = new int[size];

However, I'd suggest using lists (more info here) since they can be resized dynamically!

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

6 People are following this question.

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

Related Questions

Filling a large public array in editor 1 Answer

Public string/GameObject not in inspector 1 Answer

How to change array orders in the inspector 1 Answer

Saving values generated in game to use in prefabs. 2 Answers

How to access gameobject array set in inspector? 2 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