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 MigLeader · Dec 02, 2011 at 09:24 AM · gameobjectarrayarrayslength

Array.length edited using scripts

hi my friends , i have little problem regarding arrays , i have any array that returns some game objects

 function Awake(){
   var goh = GameObject.FindGameObjectsWithTag("Hanger");
   Hangers = goh;
 }

it works fine and it returns about 12 GameObject, i want an another variable (INT) to change the length of this array on the fly , and removes the objects also from the Array.

i tried doing this:

 function Start(){
     Hangers.Length = Fightercount;
 }

but i get this error:

 Assets/Carrierscripts/LaunchFighter.js(32,9): BCE0053: Property 'System.Array.Length' is read only.


is there a workaround for this problem, thx in adv.

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
1
Best Answer

Answer by Eric5h5 · Dec 02, 2011 at 10:07 AM

As the error says, the length is read-only. You can't change the length of arrays. If you need to add/remove objects from an array, use List (with Add and Remove) instead of arrays:

 import System.Collections.Generic;
 
 function Awake(){
   var goList = new List.<GameObject>(GameObject.FindGameObjectsWithTag("Hanger"));
 }

By the way, there's no point doing this:

 var goh = GameObject.FindGameObjectsWithTag("Hanger");
 Hangers = goh;

You can just do this:

 Hangers = GameObject.FindGameObjectsWithTag("Hanger");

Although you should use lowercase for variable names.

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 MigLeader · Dec 02, 2011 at 01:54 PM 0
Share

thx for fast respond , wt i wanted to do is : i have a carrier that have many hangers , i decide how many fighters i want to launch then it uses this many hangers depending of the count of fighters , wt do u suggest to do ins$$anonymous$$d of arrays??

and again thx for any help.

avatar image
1

Answer by StephanK · Dec 02, 2011 at 09:59 AM

You can't change the length of an array once it's setup. If you want to remove objects from an array you will have to make a new one with the correct length and assign the objects that you want to keep from your original array to the new one.

On another note it seems odd to look for already existent objects in a scene and then only use a few of them. If you don't need them, why create them in the first place?

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 MountDoomTeam · Oct 29, 2012 at 12:13 AM 0
Share

Stephan, this works System.Array.Resize.(arrayname, 10);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors?? 1 Answer

Cannot find the length of an array 3 Answers

How to create an array of Transforms 1 Answer

Index out of bounds even tho it shouldnt be [Javascript] 1 Answer

How To Get A Reference To All Nearby GameObjects? 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