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 zmar0519 · Apr 08, 2011 at 11:30 AM · guieditoreditor-scriptingfor-loop

Resizing an array of gameObjects

Hi, everyone. I am attempting to make a mass component adder, but when I input the number of gameobjects and press update, it doesn't make the object fields. I know why, the array is at a length of zero, so how do I resize it? If necessary, here is my code so far. Any help would be appreciated =]

class MassComponentAdder extends EditorWindow { public var objs : GameObject[]; public var Int : int; public var script : MonoBehaviour; @MenuItem("CUSTOM/MassComponentAdder") static function Init() { var window = GetWindow(MassComponentAdder); window.Show(); } function OnGUI() { Int = EditorGUI.IntField(Rect(3, 3, position.width-6, 20), "Number of GameObjects: ", Int); if(GUI.Button(Rect(3, 25, position.width/2-6, 25), "Update")) { for(i = 0; i < Int; i++) { objs[i] = EditorGUI.ObjectField(Rect(3, 25+(i*25), position.width/2-6, 25), "Object "+i, objs[i], GameObject); } } if(GUI.Button(Rect(position.width/2, 25, position.width/2 - 6, 20), "Add Rigidbody")) { for(k = 0; k < Int; k++) { if(objs[k] != null) { objs[k].AddComponent(Rigidbody); } } } if(GUI.Button(Rect(position.width/2, 45, position.width/2 - 6, 20), "Add Light")) { for(j = 0; j < Int; j++) { if(objs[j] != null) { objs[j].AddComponent(Light); } } } if(GUI.Button(Rect(position.width/2, 65, position.width/2 - 6, 20), "Add Camera")) { for(l = 0; l < Int; l++) { if(objs[l] != null) { objs[l].AddComponent(Camera); } } }

 }

}

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

2 Replies

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

Answer by efge · Apr 08, 2011 at 11:43 AM

Your GameObject array is a builtin array so you have to recreate the array and resize it before you assign individual elements:

objs = new GameObject[Int];
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 zmar0519 · Apr 08, 2011 at 11:59 AM 0
Share

I tried that, but it didn't work.

avatar image
0

Answer by Proclyon · Apr 08, 2011 at 11:40 AM

resizing an array? You'd have to make a new one then. They have a fixed value.

Here's quote from a wiki post on the exception:

In programming, a variable-length array (or VLA) is an array data structure of automatic storage duration whose length is determined at run time (instead of at compile time).

Programming languages that support VLAs include Ada, APL, COBOL, Fortran 90, C (added in C99) and C# (as unsafe-mode stack-allocated arrays).

And you don't want to use the unsafe keyword unless you know ABSOLUTELY sure what you are doing and why (just like meddling with matrices internal library references and quaternions).

So basically , are you sure you want to do it that way? Consider other options!

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

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

No one has followed this question yet.

Related Questions

Any way to invisibly tag an object? 1 Answer

Trigger an event from editor script? 0 Answers

SetReplacementShader in Editor Utility? 0 Answers

EditorApplication.update resetting 3 Answers

Editor Control Automagically "Filling" in Data 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