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
3
Question by thenachotech1113 · Nov 05, 2014 at 11:00 PM · arraysize

how to set an array length?

hello everyone, so i got the following arrays:

     public GameObject[] planets;
     public Vector3[] planetsPos;
 

they are part of a scritp that is supposed to spawn a random amount of planets around a sun, these guys wont work unless i manualy input their, size and since the value is random it becomes imposible. if someone knows how to set their length to the size of the planets (which would me stored in a variable) please tell me.

thanks a lot.

Comment
Add comment · Show 3
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 Scribe · Nov 05, 2014 at 11:08 PM 2
Share

I'm a little confused, arrays are supposed to be of fixed size, soyou either set it to an upper bound that you know you won't pass, set it to the exact value, or use a list.

 void Start(){
     planets = new GameOject[100];
 }
avatar image thenachotech1113 · Nov 05, 2014 at 11:24 PM 0
Share

sorry, forgot to add the random number is only generated 1 time for the script to have that parameter to generate the planets in the system

avatar image thenachotech1113 · Nov 05, 2014 at 11:26 PM 0
Share

thanks for the answer anyway

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by SarperS · Nov 05, 2014 at 11:28 PM

You can either re-initialize the array like @Scribe mentioned. Or you can use the Array class's Resize static generic method.

 Array.Resize(ref planets, newSize);
Comment
Add comment · Show 3 · 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 LtKelleyUSMC · Jul 23, 2017 at 02:51 PM 1
Share

@Sarper-Soher Excellent... There are always surprises in this program$$anonymous$$g... LOL I suppose one can never know all the tools there are available. Used your example. Worked perfect. Thanks!!!

avatar image Bubinga_Studios · Apr 17, 2018 at 09:04 PM 1
Share
 public Transform[] Look;
 [SerializeField] public RaycastHit2D[] hit;
 
 void Start() {
 hit.Resize(ref Look, 1); //Not working :(
 }

Okay, so I cannot seem to understand how to resize to another variable. plz help again lol

avatar image SarperS Bubinga_Studios · Apr 17, 2018 at 09:22 PM 1
Share

You are trying to resize the hit array to match the size of the look array I presume? Here you go

 Array.Resize(ref look, hit.Length);

Resize is a static method of the .net Array class thus you can't access it through an instance. First parameter is the array to resize passed by reference, the Resize method will create a new array of the requested size and copy your array into it using Array.Copy and set your reference array back into this new array.

Second parameter is the new size, here we're just passing the size of your second array. Below is the .net doc link, read it for more details.

https://docs.microsoft.com/en-us/dotnet/api/system.array.resize?view=netframework-4.7.1#System_Array_Resize__1___0____System_Int32_

btw You don't need to use the attribute SerializeField on public variables, they are already serialized.

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

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Array - 'transform' is not member of Object 1 Answer

Tree size problem when using terrain tool 0 Answers

Parsing XML - appropriate array size 3 Answers

System.Array.IndexOf 1 Answer

Make an array with different variables... 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