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 /
avatar image
0
Question by rsoneriu · Feb 15, 2014 at 07:06 PM · javascriptarrays

JS public array with starting values

Hello, I looked everywhere for an answer to this but I couldnt find any. I am trying to create a public array that starts with a few key/indexes. Doesnt pretty much matter which type. The thing is it doesnt save any of the values and it doesnt show in the inspector. The inspector either shows nothing or an array where I can define its size. So far I tried numerous approaches, including but not limited to: public var test : int[] = new Array(key: values); public var test: int[key:values]; public var test : int[4]; test[0] = 0... and so on. Is there a type to make a public array with a size and some values in it? or how is this done in unity? Many thanks!

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 robertbu · Feb 15, 2014 at 07:11 PM

The thing to know about public variables in JS is that any initialization is only executed when the script is attached. So changing code after the script is attached will not change the values in the Inspector. So take this script:

 #pragma strict
  
  var array : int[] = [1, 2, 3, 4];
 
 function Start() {
     Debug.Log(array.Length);
 }


If you attach this to a game object in the inspector, you will see an array of four items with the values 1,2,3 and 4. But if later you then edit those items to say 2, 4, 6, and 8 in the script, the values will stay 1, 2, 3, and 4 when you examine the variables in the Inspector. In the Inspector, in the upper right of each component, there is a gear dropdown. You can select 'Reset' from this list and Unity will reinitialize all the variables as if the script was just attached. Note when you do this, you will lose any variables you initialzied by drag and drop to that component.

Comment
Add comment · Show 4 · 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 rsoneriu · Feb 15, 2014 at 07:33 PM 0
Share

$$anonymous$$any thanks for your quick answer. However, my question was how can I create an array with some values that will also show in the inspector (not on play). For, if I create an array with var array : int[] = [1,2,3] the inspector will show the variable array and it only says size 0. So not 4 fields with some values in it.

avatar image robertbu · Feb 15, 2014 at 08:01 PM 0
Share

I know what you asked. This code will show the initialized array right after you attach it to a new game objct:

  • Create a new game object

  • Drag and drop the above script onto a game object

  • Look at the component in the Inspector

The 'Start()' is not necessary. It was just so that if you immediately run the app, you would see the values are initialized.

avatar image rsoneriu · Feb 15, 2014 at 08:16 PM 0
Share

It must be something that i do wrong then. Because the only thing i see in the inspector is the array and when i press on it it says size 0. And also i would vote up your answer for your effort but i dont have enough reputation :((( my console logs the following: System.Int32[] UnityEngine.Debug:Log(Object) CubeController:Start() (at Assets/CubeController.js:10)

avatar image rsoneriu · Feb 15, 2014 at 08:55 PM 0
Share

Ok, I'm so sorry for being so narrow $$anonymous$$ded. If i press on reset (As you said) it shows my values. Sorry again and many many thanks for your effort!

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

18 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

Related Questions

Type function(): Object[] does not support Slicing 1 Answer

How to set parents of all objects in an array? 1 Answer

Change rigidbody values on mutliple objects (ARRAY) 1 Answer

How do to you save time for each player and display it? 2 Answers

Array of custom class objects, possible? 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