I'm Trying to Access a Custom Class and can't quite get the overload technique.

I created a custom class called “City” and a list called “city” and I add a city name, population, and location (Vector3) to the list and I want to access the Index number of the list…but I’m getting an overload error: “The best overloaded method match…has some invalid arguments”

Here’s my list:

public List<City> city = new List<City> ();

Here’s my class:

public class City
{
	public string name { get; set; }
	public int population { get; set; }
	public Vector3 location { get; set; }

	public City (string n, int p, Vector3 v)
	{
		name = n;
		population = p;
		location = v;
	}

I want to access the Index number of the list from time to time so I can use the information elsewhere. I use (successfully) “IndexOf()” in another script to access the index of a different class item in a different list…and that works fine…but it’s a single item class so it’s easy to call.

I can’t quite seem to get something right here, not sure if it’s the syntax or something peculiar with Unity 5 (or a programmer malfunction), or what.

		int temp99 = GetComponent<SpawnCity> ().city.IndexOf("I've tried everything here and nothing seems to work");

		Debug.Log (temp99);   

Again, I can get IndexOf to work on my other list in another script but it’s a single integer class…for the life of me, I can’t get this three item class to work.

Hmmmm.

When you exactly know the properties of an object you are trying to find in a list, you can create an instance of an object, set the properties of the object with the know values, and pass it to the IndexOf() method, to find it in the list.

For example, if you know that you are looking for a city with the name of ‘CoolCity’, which has a population of 1000 people and the location is (0, 0, 0), you should write:

int index = city.IndexOf(new City("CoolCity", 1000, new Vector3(0, 0, 0)));

However, in this situation you are identifying the object by clicking on the gameobject in the scene. Looking at your code above, i can see that you name the gameobject with the name of the city after instantiating it. By knowing only one property of the city (the name), you should not use IndexOf(), but you should use your own method which iterates through the list to find a matching city.

// By default, this is -1 - if the method can not find the matching city in the list, it will return -1
int index = -1;
for(int i = 0; i < city.Count; i++)
{
	if(city*.name == "CoolCity")*
  • {*
  •  index = i;*
    
  •  break;*
    
  • }*
    }
    This will return the index of the first city in the last named as ‘CoolCity’. Note that this also means that every city has to have a unique name. This method can be easily converted into other methods: finding a city by population, or by location. If you include a && operator and check an other property, you can also search for combinations. Your only job is to insert this code into the function which gets called after clicking on city gameobject, and replace ‘CoolCity’ with the name of the city you are looking for.

I figured it out. Apparently, the problem wasn’t with the Overload although for some reason that is what the message indicated. I was using the wrong “index” finder. Instead of “IndexOF()” I should have been using FindIndex. This line eliminated the error and makes things work.

		int testNum99 = city.FindIndex(x => x.name == "Megaton");

This returns “14” which is the last city I add to my list of 15…which is the correct index number.

Sometimes I don’t fully understand the question so it may not always be clear what I’m asking for. In that event, I apologize.

I do have the answer finally. Took me a couple days to find it. But at least now I can move on.