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 boddole · Feb 25, 2014 at 09:02 PM · c#transformarrayarrays

Conceptual Question on Creating Arrays at Runtime

Hello everyone, I've been needing to do some work with arrays that store data types like Transforms on gameobjects, scripts on gameobjects, etc at runtime. My current understanding is that if I wanted, say, an array of Transforms, I would first need to create an array of gameobjects then pull the transforms (or I guess Vector3) data from that first array, since you cannot use methods like getcomponent() if you are doing say find.objectswithtag, since casting or using "as" doesn't seem to work.

Is my understanding correct? If not, what is the best way to go about creating these types of arrays at runtime?

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
2

Answer by knowpixels · Aug 02, 2014 at 02:03 AM

There are 2 types of Arrays in Unity Script.


The "Built In" arrays, such as Vector3[] and GameObject[]. These builtin arrays run very fast and you can create one at runtime with code using a runtime declaration such as

var ga:GameObject[]; ga = GameObject.FindGameObjectsWithTag("Collectables");

That would declare and then populate a builtin array called ga with all the gameobjects tagged Collectables. Built in arrays are the ones you see in the inspector. The restriction is the array size or length cannot be altered at runtime.


The native javascript arrays which use the standard declaration syntax

var ga:Array = new Array(Vector3(0,0,0),Vector3(0,0,0));

Native JS arrays are no where near as fast but they are flexible so you can push (Add) and splice and all things nice :)


When it comes to deciding which one to use I would recommend using built in arrays for the sheer speed of them when you know you will not need to change the array size or length. However if your array will require total flexibility at runtime and you are willing to take a performance hit then go for a native JS array.

There are scenarios where you may need to create a native JS array at runtime and dynamically populate it with say Vector3 instances that are randomly generated and then take the dynamic values of your new native JS array and copy them into a new built in array. My use case was for creature AI patrol points random generation and regeneration logic.

To pass my random dynamic values in my native JS array into a unity built in array at runtime I used the ToBuiltin() function from documentation :

randomPatrolWayPoints = tempArray.ToBuiltin(Vector3) as Vector3[];

So 2 types of array, built in and native JS array. Built in is faster but inflexible and native arrays are slower but have all the flexibility that could be required.

Also when using built in arrays the For(a:GameObject in myGameObjectsArray){} is faster that the traditional for loop.

^_^

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 flaviusxvii · Aug 02, 2014 at 02:08 AM 0
Share

You realize you are answering a question from February?

avatar image knowpixels · Aug 02, 2014 at 02:14 AM 0
Share

Yarp ;) must be feeling helpful as I am 34 today.. Happy birthday to me, because all my arrays were builtin Vector3... heh heh :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

21 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

Related Questions

Array empties values on RunTime? 1 Answer

C# Children and SubChildren Null Reference Exception 1 Answer

How can i create array of texture2d with variables names ? 2 Answers

[C#] Buttons through an Array. 1 Answer

How to merge an arrays into arrays inside an array? 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