Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by nikocr33nzz · Jan 02, 2017 at 03:44 PM · c#arrayarraylistarray list

arrays basic ! why my array size is always changing?

i have this little problem. i have array and i already set the size in declaration. but when i try put the variable to that array , the size of the array is always changing. this is the example code :

 public GameObject[] enemy = new GameObeject[4]; // i also set the size 4 in the inspector
 
 
 void update()
 {
    enemy = GameObject.FindGameObjectWithTag("Enemy");
 } 


as you can see the example code. i try to put the gameobject enemy into the array. but i cant do it in the inspector because the enemy is not created just yet. with the code above the size of enemy is always change. what i want is when the one of the enemy index is null , it is not change the size? anything would help. thanks

Comment
Add comment · Show 2
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 dpoly · Jan 03, 2017 at 12:22 AM 0
Share

This is a very basic question about C# arrays and has nothing to do with Unity. $$anonymous$$gest you try this tutorial https://www.tutorialspoint.com/csharp/csharp_arrays.htm. Even better, use lists (but do a tutorial first).

avatar image nikocr33nzz dpoly · Jan 03, 2017 at 02:06 AM 0
Share

i know its very basic , thats why i mention it in the title and thanks for the suggest . i'll do more tutor

2 Replies

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

Answer by Creeper_Math · Jan 02, 2017 at 06:55 PM

You need to make it so every update, the array is compared with a new array (with new entries), and those entries are ADDED if they don't have an equal one in the original one

 void Update() {
      // Get a new array with an updated list of gameobjects
      GameObject[] UpdatedArray = GameObject.FindGameObjectWithTag("Enemy");
      // Go through the updated array and if it cannot find an equal object in the other array, then ADD itself to the array
      foreach (GameObject itemUpdated in UpdatedArray) {
           bool Original = true;
           foreach (GameObject itemOriginal in enemy) {
                if (itemUpdated == itemOriginal) {
                     Original = false;
                }
           }
           if (Original) {
                // My "complex" script for ADDING to an array
                GameObject[] AddOriginalArray = enemy;
                enemy = new GameObject[AddOriginalArray.length + 1];
                int Selection = 0;
                foreach (GameObject item in AddOriginalArray) {
                     enemy[Selection] = item;
                     Selection += 1;
                }
                enemy[Selection] = itemUpdated;
           }
      }
 }
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 nikocr33nzz · Jan 03, 2017 at 02:01 AM 0
Share

thank you very much @Creeper_$$anonymous$$ath . it worked perfectly . just like i want it

avatar image Creeper_Math nikocr33nzz · Jan 03, 2017 at 09:40 PM 0
Share

Just make sure that every time you use a "foreach" on the array, include an if (item != null) to it, or the script will just stop and not continue!

avatar image
0

Answer by RobAnthem · Jan 02, 2017 at 07:16 PM

 enemies = FindObjectsOfType<enemy>();


will return all your enemies, and recreate the array to the new size every time. Arrays are basically terrible in comparison to Lists because of the dynamic aspect, an Array cannot be added to, only its entries can be changed. Even a list is just an Array with more features. RAM is allocated to vars on the creation of them, as such EVERY time an array needs the size altered, it copies the contents over and creates a new array in a new RAM address. As such I highly suggest switching to lists, and having an overall enemy list which can be empty on the start and every enemy could have this on his script

 void OnEnable()
 {
     if (npcHandler.enemies == null)
     {
         npcHandler.enemies = new List<enemy>();
     }
     npcHandler.enemies.Add(this)
 }


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 nikocr33nzz · Jan 03, 2017 at 02:32 AM 0
Share

i want to change it to list but since i already made that array and the array is linked to other script and functions so i am looking some ways to fix the array.

but if i used list how can i put multiple instantiate gameobject to the list ? is it can be done like this ?

 void create_enemy ()
 {
      enemies = new List<Gameobject>();
 
     for (int i = 0 ; i < 4 ; i++)
     {
     Instantiate (enemyBlue.enemyBlueObj, spawnPosition, spawnRotation);
     enemies.add (this);
      }
 
 }

or i have to declare the gameobject first like this

 void create_enemy ()
 {
      enemies = new List<Gameobject>();
 
     for (int i = 0 ; i < 4 ; i++)
     {
     GameObject Enemy = Instantiate (enemyBlue.enemyBlueObj, spawnPosition, spawnRotation) as Gameobject;
     enemies.add (Enemy);
      }
 
 }

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

294 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 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 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 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 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 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 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 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 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 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

Just another line renderer question. Line renderer vector positions in an array of game objects. My line only draws between 2 positions? 1 Answer

Adding an element to an array via script C# 0 Answers

Is it possible to have 2 references in an array? 1 Answer

Why arrays don't set on void Start() ? 1 Answer

Keep track of last audio clip played from multiple arrays 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