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 Topthink · Jan 07, 2017 at 11:19 PM · listclassindexsyntaxoverload

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.

Comment
Add comment · Show 16
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 hexagonius · Jan 07, 2017 at 11:27 PM 0
Share

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.

avatar image Topthink hexagonius · Jan 07, 2017 at 11:34 PM 0
Share

Thanks, I'm having a cat issue right now and it will take a $$anonymous$$ute to resolve...I'll be right back.

avatar image Topthink hexagonius · Jan 07, 2017 at 11:42 PM 0
Share

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.

avatar image Topthink hexagonius · Jan 07, 2017 at 11:47 PM 0
Share

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.

Show more comments
avatar image Topthink · Jan 07, 2017 at 11:30 PM 0
Share

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.

avatar image KoenigX3 · Jan 07, 2017 at 11:40 PM 0
Share

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].

avatar image Topthink KoenigX3 · Jan 08, 2017 at 12:03 AM 0
Share

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.

avatar image Topthink KoenigX3 · Jan 08, 2017 at 12:06 AM 0
Share

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.

avatar image Topthink KoenigX3 · Jan 08, 2017 at 12:31 AM 0
Share

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.

avatar image Topthink · Jan 08, 2017 at 12:09 AM 0
Share

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.

avatar image Topthink Topthink · Jan 08, 2017 at 12:15 AM 0
Share

I tossed the Vector3 part of the problem just to simplify it and I'm trying to figure it out like that.

avatar image Topthink Topthink · Jan 08, 2017 at 12:28 AM 0
Share

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.

avatar image Topthink · Jan 08, 2017 at 12:18 AM 0
Share

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)));
avatar image Topthink Topthink · Jan 08, 2017 at 12:25 AM 0
Share

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.

Show more comments

2 Replies

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

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.

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 Topthink · Jan 09, 2017 at 12:46 AM 1
Share

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.

avatar image
0

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.

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

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

63 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

Related Questions

A node in a childnode? 1 Answer

How do I return the index of an array of sprites as an int? 1 Answer

Edit each Parameter Class on Function 1 Answer

missingGameObject problem 3 Answers

Loop through a GameObject list based on the size of the list 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