• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Monko · Dec 18, 2013 at 02:22 AM · arrayslot

Find first empty "slot" in array

I am trying to make a function (in C#) that will return the first "empty" slot in an array. Lets pretend I have an array that looks like this:

 testarray[0] = Vector3(7,4,2);
 testarray[1] = Vector3(9,12,5);
 testarray[2] = Vector3(6,15,1);
 testarray[3] = Vector3(0,0,0);
 testarray[4] = Vector3(0,0,0);
 testarray[5] = Vector3(13,8,5);
 //...

In this example, the function would return 3, because 3 is the first time testarray contains an "empty" Vector3.

My attempts involve a for loop, but I still can't get it to work. Ill post my code so far, but if anyone has any other ideas, please help.

 int FindFirst(Vector3[] array)
 {    
     for (int i = 0; i <= thearraylength; i++)
     {
          if (array[i] == new Vector3(0f,0f,0f))
          {
          return i;
          break;
          }
     }
 }
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

3 Replies

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

Answer by Eric5h5 · Dec 18, 2013 at 03:07 AM

Don't use for loops, just use IndexOf:

 int firstEmpty = System.Array.IndexOf (testarray, Vector3.zero);
Comment
Add comment · 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
0

Answer by Monko · Dec 18, 2013 at 02:59 AM

Wow, how embarrassing. After 3 hours of trying to figure this out, I post a question. Then 5 minutes later, I solve it by myself. I solved it, and becuase I don't think it is possible to delete this post, I might as well show my answer. As it turns out, I was really close.

 int FindFirst(Vector3[] array)
 {   
     for (int i = 0; i <= thearraylength; i++)
     {
          if (array[i] == new Vector3(0f,0f,0f))
          {
          return i;
          break;
          }
     }
 return -1;
 }

This function will return the first location in the array of Vector3(0,0,0). If it is not found, it will return -1.

edit- After looking at Erich5h's answer, he has a much easier, and probably faster solution. Thank you Eric5h5.

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 Hoeloe · Dec 18, 2013 at 12:33 PM 0
Share

I think it's worth pointing out something. Vector3 is a struct, meaning (in C# anyway) that it's a value type. This means that it cannot ever be uninitialised. Declaring it implicitly initialises it with some default value (in this case, it's Vector3.zero). If the object in question were a class, then declaring it will just initialise it to null, so you would have to do a null test ins$$anonymous$$d of a default test.

On another note, Eric5h5 gave a more elegant solution, as the method in question already exists, and is probably more efficient than your implementation.

avatar image Monko · Dec 18, 2013 at 05:02 PM 0
Share

Yup, Eric5h5's solution is wwwaaayyyyyy easier. He nailed it.

avatar image
0

Answer by KellyThomas · Dec 18, 2013 at 03:01 AM

Try using this as your equality test: array[i].Equals(Vector3.zero)

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 Eric5h5 · Dec 18, 2013 at 03:14 AM 0
Share

There's no reason to do that; Vector3 is a struct and you would typically use == for the sake of readability. Although it would be slightly more efficient to reference Vector3.zero rather than creating a new struct, so ideally it's array[i] == Vector3.zero. (Leaving aside the fact that the whole thing's moot because IndexOf exists. ;) )

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How to go about mounting weapons on spaceships and storing these. 1 Answer

Check array slot against corresponding slot in another array? 1 Answer

Alter a GameObjects(Clone) array from another script C# 0 Answers

Return reference to object added to an array (Javascript) 1 Answer

Array.map Support 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