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 Kennyist · Oct 19, 2013 at 10:36 PM · c#gameobjectarrayforloop

GameObject array problems c#

I'm having issues with arrays, Im trying to sort and remove elements from an array if they are too far away, But what ever method I try it does not work. I'm trying a for loop at the moment, but it only loops once, even though the debug shows it has a length of 3.

alt text

 targets = GameObject.FindGameObjectsWithTag("Targets");    
             
 Debug.Log("l = "+targets.Length);
                     
 for(int i = 0; i <= targets.Length; i++){
                 
     //if(Vector3.Distance(heli.position,targets[i].transform.position) < targetRange){
         Debug.Log ("item "+i);
         tempTarg[i] = targets[i];
     //}
 }
         
 targets = tempTarg;
             
 target = targets[Random.Range(0, targets.Length)].transform;
 

Anyone have any idea about this.

Also there are references to an array.Remove function, But it does not appear for me, unless it doesn't exist for C#. also just seem to be having general issues with arrays, like .Length does not appear in the method list.

untitled.png (5.5 kB)
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 Hoeloe · Oct 19, 2013 at 10:44 PM 1
Share

I notice one problem in your for loop: i <= targets.Length will cause problems. Arrays are 0 indexed, which means that you get the elements of the array by calling array[i], where i is a value between 0 and array.Length*-1*, inclusive. By using

Also, you can't remove objects from an array - .NET arrays are of static length. This allows them to have extremely high performance, but means you can't resize them. I recommend using a List structure if you absolutely must resize the collection.

Also, the Array class is not the same thing as arrays. In C#, the Array class holds extra methods for use with arrays, while most general things like Length are just members of array instances.

avatar image robertbu · Oct 19, 2013 at 10:49 PM 1
Share

You may want to research the different collection types.

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F

Remove() is not part of the built-in array. There is a RemoveAt() in the .NET generic list, but that is not what FindGameObjectWithTag() returns.

One issue I see (which I don't think is at the root of the issue you describe) is that your for() loop executes one too many times. It uses '

 for (int i = 0; i < targets.Length; i++) {

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers

Read different game objects into array at same time? 2 Answers

How to select an Game Object from an Item List 0 Answers

How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 Answers

NullReferenceException error in an array of objects 0 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