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 CallToAdventure · Apr 10, 2012 at 10:12 PM · transformarray

Array of transforms

Found little asking my old friend Googol so I am asking here : how do I create an array of targets (in my case transforms)?

I have this group of objects - defined in a public variable. But along the way, as I progress trough levels I want to activate more groups of objects (or targets); Or change them - activate or deactivate depending on level. So what I use now is :

var _targets:Transform;

And in the editor I assign it nicely. But I want more than one. Please link to some good docs:)

Comment
Add comment · Show 4
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 Lttldude · Apr 10, 2012 at 10:25 PM 0
Share

Did you mean var _targets:Transform[];?

Are you wanting to add transforms to an array while you are playing? If so, then you'll have to convert _targets to a js array first, so then transforms can be added or removed, then converted back into a builtin array when you want to use the transforms in your script.

See the examples on this page http://unity3d.com/support/documentation/ScriptReference/Array.html

avatar image CallToAdventure · Apr 10, 2012 at 10:41 PM 0
Share

found this: http://answers.unity3d.com/questions/46468/how-do-i-create-an-array-for-multiple-targets.html

and it works well, I suppose I can activate/deactivate stuff on the go; problem is I need to figure out how to specify more than one group in the array (or the entire length):

Now I am using var _targets:Transform[]; .....

if ( _targetsHit == _targets[1].childCount && _resetLevel == false )

but if I use _targets[_targets.Length] I get an array out if index error. Also, wouldn't this give more errors if I have a group in the array inactive at one point?

I'll check the link in the answer thanks!

avatar image by0log1c · Apr 10, 2012 at 10:45 PM 0
Share

The length of an array always returns last index + 1. Remember the array's index starts at 0. So an array holding [1,2,3] has a length of 3, but the last index is 2.

avatar image CallToAdventure · Apr 11, 2012 at 10:37 AM 0
Share

Sorry, so if I have two targets in my array, i use [1,2] or [0,1]? either gives me errors. Or putting it different, how do I add changes to all targets in my array?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · Apr 10, 2012 at 10:28 PM

UnityScript offers two different types of arrays:

  • Dynamic arrays, with the Array class (you can resize these at will)

  • Static arrays, with traditional "built-in" arrays (these have a fixed length)

The official script reference actually has a pretty good page explaining the two, including some examples.

Creating a built-in array is simple:

 var _targets : Transform[]; //note the brackets

Creating a dynamic array is also simple:

 var _targets = new Array(); //note the parens

From there, you can populate the array from the inspector and reference the elements of the array in your code.

If you're going to be resizing the array from code on a regular basis, you might want to use Array. If not, you may want to use a built-in array. It's really up to you.

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 by0log1c · Apr 10, 2012 at 10:43 PM 1
Share

I'll add that Array() are UnityScript only. I'd avoid them as the object they holds aren't typed, which makes it less performant. To have a resizable array-like variable, take a look at the System.Collections.Generic.List class.

avatar image Kleptomaniac · Apr 11, 2012 at 11:05 AM 0
Share

I would look here ins$$anonymous$$d of the docs ... much better info covering all types of arrays ... also, as @BY0LOG1C said, you should most generally avoid Javascript Arrays. They are horrible for performance. Go with Generic Lists ins$$anonymous$$d. :)

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

7 People are following this question.

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

Related Questions

Sorting an Transforms array by gameobject name 1 Answer

problem moving a prefab object in script (c#) 0 Answers

Unity3D - Playback object array of position (with dynamic velocity) 0 Answers

Getting radius around multiple transform 1 Answer

How to have a GameObject follow a path with c#? 3 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