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 /
avatar image
0
Question by GrafSpee · Jul 09, 2017 at 01:00 AM · arrayssearchcustom class

Search for the index in an array of a customised class

I need to find the index of a customised class using only a value in one of the three values that I stored in the class.

         city = new City[34];
 
         city[0] = new City("Basilus", 1000, 2834);
         city[1] = new City("Gortz", 3000,2455);
         city[2] = new City("Daniz", 4500,1768);

I want to use the right most value to search for its index.

The City class looks like this:

 public class City{
     
     string cityName;
     int cityPopulation;
     int cityCoords;
 
     public City(string cityName,int cityPopulation,int cityCoords)
     {
         this.cityName = cityName;
         this.cityPopulation = cityPopulation;
         this.cityCoords = cityCoords;
     }
 
     public string CityName()
     {
         return this.cityName;
     }
 
     public int CityPopulation()
     {
         return this.cityPopulation;
     }
 
     public int CityCoords()
     {
         return this.cityCoords;
     }
 }

I tried using the indexOf code but it wont work on this one because I need it to be a method for it to return a value. I've tried to create a function that will loop the array until it gets the correct index but it's saying " use of unassigned local variable 'index' ".

     int GetIndexUsingCoords(int hexCoords)
     {
         int index;
         for (int t = 0; t < 34; t++)
         {
             if (hexCoords == DefaultCityData.city[t].CityCoords())
             {
                 index = t;
             }
         }
         return index;
     }

Is there a way to search for the index directly without using a loop or I will need to stick with the loop but just change some parts. Please help out, I've been working on it for the last couple of hours.

Comment
Add comment · Show 1
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 durnurd · Jul 09, 2017 at 02:31 AM 0
Share

Implementation note on the City class: Those fields and getter methods look like good candidates for being turned into readonly fields, or properties:

 public class City{
      public string CityName {get; private set;}
      public int CityPopulation {get; private set;}
      public int CityCoords {get; private set;}
  
      public City(string cityName,int cityPopulation,int cityCoords)
      {
          CityName = cityName;
          CityPopulation = cityPopulation;
          CityCoords = cityCoords;
      }
 }

1 Reply

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

Answer by Jwizard93 · Jul 09, 2017 at 02:02 AM

Looks fine to me except you need to initialize it to something. There's a chance no city is found with the correct hexCoords. In that case what is being returned?

At the top set index = -1.

That way the caller can see if the index came out to be between 0 and 34. Otherwise it can disregard it or throw an error or anything.

Alternatively use a list instead of an array and there will be more functionality already built in.

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 GrafSpee · Jul 09, 2017 at 02:35 AM 0
Share

Lol, the answer was so simple. I set index to -1 and it worked.

I changed my array into list then tried to search for it.

 int cityIndex = DefaultCityData.city.IndexOf(hexCoord);

but this line of code will not work it says, cannot convert hexCoord from int to city. What else am I missing on this line of code? I just want to search for the index using the 3rd value in the list.

avatar image GrafSpee · Jul 09, 2017 at 04:55 AM 0
Share

After digging through some list codes. I found the answer that I needed.

 Debug.Log(city.FindIndex(x => x.CityCoords() == 5769));

Thanks a lot for the suggestion to convert it to list! :)

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

69 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

Related Questions

Error referencing custom class elements 0 Answers

How can I check values of all array/List quickly 1 Answer

For loop not running correctly inside start () 1 Answer

Using arrays and for loop with GetComponent error 1 Answer

How to select a texture in an inventory? 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