- Home /
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.
IndexOf is getting you the list index for an item of that list. I don't know what you want to achieve, then I'd help.
Thanks, I'm having a cat issue right now and it will take a $$anonymous$$ute to resolve...I'll be right back.
Sorry, I'm back. I have a slightly tubby tabby who insists on sleeping on my keyboard...causes some problems occasionally. Let me post something up for you.
I'm spawning some objects at random locations (primitive cubes that represent cities in this case but I'm going to spawn other things too) and I want to be able to mouse-click on the object and have the GUI post up information about it on my screen.
Usually, I try to call the list by city.IndexOf("CityName"); but I get the error that the best overload is invalid. I've tried all sorts of stuff in the "CityName" but I can't seem to ask for what is required.
Are you trying to search for a specific City element in the city list? So you know the properties of the city, but you want to find it in the list (if it exists there), and get the index number? Then you should use 'IndexOf(cityObject)', where the 'cityObject' is a City-type variable which should be constructed before being used. Or you could say 'IndexOf(new City(name, pop, loc))' and fill 'name', 'pop', 'loc' with the data you are looking for.
However, if you are trying to return the City object using a known index, you should just simply use city[index].
I can see the city on my map and I want to click on it to get some information. That information right now is stored in a list.
Eventually, I'll have all sorts of objects on the map and it's all stored in class/lists. I want to be able to click on anything I can see to get information about that object.
When the class/list has only a single item stored on it, say an integer, I can retrieve it with no difficulty. But when the class/list contains numerous items (say, a string/name, and an int/population, and a vector3/location), then I'm not able to click on the city to get any information...I get the "overload" message.
I'm not really wanting anything "specific" ... I will have several items that is listed eventually...maybe ten or twelve items or so. The thing I'm looking for might be any one or more of the items from the list. What I want to see might be different depending on the circumstances. So I want to display all the items every time.
I'm going to get rid of the vector3 and just have a string/name and a integer/population and see if I can figure out how to get the index of that class/list item.
I'm not sure what is causing my problem here.
But, I can get the index of a list/class with a single integer in it. But when I add another item to the class, I seem to be co$$anonymous$$g up short. Again, I'll get rid of the vector3 and see if I can figure it with just the string/name and the integer/population.
That might tell me what my problem is.
I tossed the Vector3 part of the problem just to simplify it and I'm trying to figure it out like that.
I tossed the Vector3, totally eli$$anonymous$$ated it from the list, but I get the same error. Hmmmm. I was hoping that by simplifying the class/list I could figure out what the problem was........Not!!!
I'm still trying to figure this.
Here's how I add the information to the list. I manually add 15 cities which are primitive cubes. The class is fine. The list is fine. No problems adding the cities or anything. They all print on my map just fine.
Instantiate (CityCube);
CityCube.transform.position = new Vector3 (UnityEngine.Random.Range(25,75), 0.05f, UnityEngine.Random.Range(25,75)); /// Corner
CityCube.name="Epsilon";
city.Add(new City("Epsilon", UnityEngine.Random.Range(1000,1250)));
I'm adding a simple prefab "cube" that shows up on my map as a cute little yellow square. I'm giving it a name "Epsilon" and I'm assigning it a random population.
But I want to click on that city and I want it to display the city name and population (and all the other eventual information) to a GUI box or label.
Answer by KoenigX3 · Jan 08, 2017 at 06:55 PM
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[i].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.
Thanks. I probably wasn't as clear as I could have been. I figured out what I need by using your answer and putting a slight spin on it.
I appreciate you taking the time to help me.
It took two days for me to figure this out. It might have taken another two weeks for me to figure it out without your help.
Answer by Topthink · Jan 09, 2017 at 12:37 AM
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.