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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Jip · Mar 15, 2014 at 11:38 AM · arrayinspectorvalues

Arrays and the inspector

Dear community,

I've got a question related to initialize arrays through the script so that they already have the values in the inspector before the game launches. (Aka: You can edit them on the spot, in the inspector.)

Now, when I read the question I don't understand myself what I'm exactly asking, so allow me to elaborate my question, lets say we got this piece of code:

 // Lots of other code
 var tileSignal : int = 5;
 // Lots of more code

Now, if I would assign this script to a new Gameobject, it would automatically get the value 5 in the inspector.

However, when the variable is an array, I can't seem to be able to initialize it through the script, for example:

 // Some strange code above
 var arrayPosZ : float[];
 // Lots of code below

I can't find a way to initialize this the same way as the non-array variable (everything that I've tried gives me errors), so that they are prefilled in the inspector when I attach the script to a new gameobject.

To be more specific, I currently got the following code, and use the Start function to make sure it is applied to all the gameobjects. However, this approach causes the inspector to be completely overridden and by doing so I can't add any "individual values" to gameobjects. I currently use the following code:

 // How to initialize this color array?
 //         >> Currently doing it in Start, so the black colors in the inspector... Will be overridden.
 var tileColors : Color[] = new Color[4];
 
 // What are our orders from our superiors? (Aka: What color will we become, or will we deselect?)
 //        >> If 4 is given as order, it means deselect. 
 var tileSignal : int = 5;
 
 function Start () {
 
 // Initializing color array.
 tileColors[0] = new Color(0.2,0.6,1.0,1.0); // Light Blue, means moveable.
 tileColors[1] = new Color(1.0,0.4,0.4,1.0); // Light Red, we got an enemy upahead.
 tileColors[2] = new Color(0.4,1.0,0.4,1.0); // Light Green, our allies are nearby.
 tileColors[3] = new Color(1.0,1.0,1.0,1.0); // Default Color, this is either white or black (depending on where the Tile is)
 
 }

All in all, my question could be phrased as the following: How do I prefil arrays like I did with the non-array integer? And in more specific, with colors.

Lots of thanks in advance, Lots of love too,

Jip & my little nephew Yue who made this really hard to type.

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

1 Reply

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

Answer by whydoidoit · Mar 15, 2014 at 11:40 AM

You need to initialize the array directly like this:

     var tileColors : Color[] = [new Color(0.2,0.6,1,1), new Color(1,0.4,0.4,1.0)];
Comment
Add comment · 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 Jip · Mar 15, 2014 at 12:01 PM 0
Share

So much frustration, such a simple answer. So much thanks!

In a similar way, lets say we have a Vector3, it would work the same way?

 var tileColors : Vector3[] = [new Vector3(0.2,0.6,1,1), new Vector3(1,0.4,0.4,1.0)];

Either way, Lots of thanks already!

avatar image whydoidoit · Mar 15, 2014 at 12:03 PM 0
Share

Yes, that's how it works.

avatar image alexander_q · Mar 21, 2014 at 03:14 AM 0
Share

What is the C# equivalent of this? I tried

 public Behave[] movementScheme = new Behave[Behave.moveDown];

and

 public Behave[] movementScheme = [Behave.moveDown];

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

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Unexpected values at the inspector when initializing member values from built-in arrays 0 Answers

How to make a custom type's contents modifiable in an array by inspector? 1 Answer

How do I make a public array of arrays appear in the inspector? 1 Answer

Multidimentional Array for Inventory style system in inspector. 1 Answer

Trying to be more efficient. Advice please... 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