Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 tatarinrafa · Mar 16, 2014 at 03:06 PM · arrayfunction

How to resize c# array of gameobjects&

I have scripts using "c# array", so replacing evrything to "c# list" will be a headache. Help with code

 ------
 if(MyUnits[MyUnits.length] != null){
 Resize(MyUnits, (MyUnits.length) + 10);
 }
 ------
 public void Resize(GameObjectsArray, NewSize){
 
 ///WHAT GOES HERE????
 
 }
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 tatarinrafa · Mar 17, 2014 at 05:57 PM 0
Share

It does not work for me

public static GameObject[] Test;

 public void GroupResize (int Size, GameObject[] Group){
  
     GameObject[] temp = new GameObject[Size];
     for (c = 1; c < Group.Length;){
        temp [c] = Group[c];
        c = c + 1;
     }
     Group = temp;
  
 void Start(){
     Test = new GameObject[5];
     Debug.Log (Test.Length);
     GroupResize(15 ,Test);
     Debug.Log (Test.Length);
 }
avatar image tatarinrafa · Mar 17, 2014 at 05:59 PM 0
Share

Debug.Log shows 5 before and after resize

avatar image Adamcbrz · Mar 17, 2014 at 08:56 PM 0
Share

Group has a local scope. You either need to pass by reference or return the results and assign it from your method return.

avatar image tatarinrafa · Mar 20, 2014 at 05:40 PM 0
Share

Can you please explain it in simple way or just paste the code. What and where i have to add to my code ??

2 Replies

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

Answer by Adamcbrz · Mar 16, 2014 at 03:15 PM

The list is you best option but to answer you question you will have to create a second array.

1) Create new array with new size.

2) Loop through old array and add each elements to new array.

3) set old array = to new array

Comment
Add comment · Show 4 · 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 Adamcbrz · Mar 20, 2014 at 07:07 PM 0
Share

$$anonymous$$odifying your code here is the results

 public void GroupResize (int Size, ref GameObject[] Group)
 {
         
     GameObject[] temp = new GameObject[Size];
     for (int c = 1; c < $$anonymous$$athf.$$anonymous$$in(Size, Group.Length); c++ ) {
         temp [c] = Group [c];
     }
     Group = temp;
 }
         
 void Start ()
 {
     GameObject[] Test = new GameObject[5];
     Debug.Log (Test.Length);
     GroupResize (15, ref Test);
     Debug.Log (Test.Length);
 }
avatar image tatarinrafa · Mar 22, 2014 at 07:52 AM 0
Share

that works! Finaly a clear answer))

avatar image Je Hwan Yoo · Jan 25, 2016 at 03:49 AM 0
Share

Good! It's the best Algorithm of resize in unity! Thank you, so much.

avatar image Bunny83 Je Hwan Yoo · Jan 25, 2016 at 07:19 AM 0
Share

Uhm, System.Array.Resize does exactly this but probably a bit more efficient ^^. Also watch out, it seems there's a typo in the script because "c" should start at "0" and not "1". Otherwise you're skipping the first element.

avatar image
7

Answer by Chronos-L · Mar 17, 2014 at 06:04 PM

Use Array.Resize( ref myArray, newSize)

Comment
Add comment · Show 5 · 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 tatarinrafa · Mar 22, 2014 at 07:53 AM 0
Share

"ref" made it to work. Thank You!=)

avatar image Mentaner · Oct 31, 2015 at 12:53 PM 0
Share

Exactly what I was looking for! Thank you!

avatar image Hamed_Kh · Dec 29, 2017 at 01:28 PM 0
Share

what about 2d or 3d array ?

avatar image Bunny83 Hamed_Kh · Dec 29, 2017 at 02:30 PM 0
Share

Array.Resize doesn't actually resize an array as arrays have a constant size once created. The Resize helper method just creates a new array with the specified size and copies the elements from the old array into the new array.


When dealing with multidimensional arrays you have to do those steps your self.

avatar image DavidSof · Jun 04, 2021 at 01:41 PM 0
Share

To use Array.Resize you must include of using System with it , and if you use Random function inyour code you must now to add Unity.Random to it

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

28 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

Related Questions

ArrayPrefs 2 (PlayerPrefs X) Question 2 Answers

Can you make an array of functions? 2 Answers

How to call a function from another script in C# from array? 1 Answer

variable with int value not passing to command 1 Answer

Cycling Texture Array 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