Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 The_Toller · Oct 13, 2015 at 06:38 PM · c#listlistsindexof

Grab a specific item from a list

Hello!

This is what I'm looking for:

 public List<GameObject> objects = new List<GameObject>();
 
 //.......
 
 void Check()
 {
    if(objects.Count() == 1)
    {
      GameObject obj = //THAT ONE OBJECT
    }
 }

How would you solve this? :)

Original post:

I have a list of selected GameObjects in my game, which I iterate through when assigning objectives etc. However, there are one objective that can only be done if only one object is selected. This is easily checked with the list.Count and so on. But if that returns "1", meaning that only one object is selected - I want to assign the objective to it. However, I have previously used a foreach-loop to skim through the objects and assigning their objective... this is of course working even when the list only contains one object, but is there an easier way to access a particular object? From what I can see, I can get the index value of certain objects, but I don't see a method where I can use that information to access the specific item.

The method will never run if the list.Count returns anything else than "1". So I want to say, okay, then grab that item and tell it to do this........etc....

Thoughts? :)

Comment
Add comment · Show 2
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 Munchy2007 · Oct 13, 2015 at 02:54 PM 0
Share

If I understand you correctly, it sounds like you might want to take a look at Dictionaries.

You can retrieve specific entries by 'key' rather than iterating through the list, so you can use the key to grab that item, and you can use TryGetValue() in instances where you aren't sure if the dictionary contains the item you are querying.

Hope this helps.

avatar image The_Toller Munchy2007 · Oct 13, 2015 at 05:54 PM 0
Share

@$$anonymous$$unchy2007 - Thanks for your answer! I have indeed taken a look at Dictionaries, though - I don't see how that would be a smooth solution since I don't really know the "key". The selection list could contain multiple copies of a type of object... Or is there a way I could check for the key for that one object and then use the key for retrieving it? The whole issue is that I can't know what object it is beforehand. All I know is that it must only be one. The foreach does not really clutter up the code... but it feels sort of clumsy when I know there's only one. :) And if I should check for a key in a dictionary, that would somehow make me forced to check for all possible keys? That would be way heavier in the code I believe? :o

3 Replies

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

Answer by Dave-Carlile · Oct 13, 2015 at 06:45 PM

You can use IndexOf to find the index of a specific object in the list...

 int index = objects.IndexOf(someKnownObjectReference);

And then use that index to grab the object...

 if (index >= 0)
 {
     GameObject o = objects[index];
 }

But... if you already have a reference to the object (which you indicated was known) then there's no need to find it in the list to modify it? Just modify the reference you already have... I'm not entirely sure what you're trying to accomplish either, and the code doesn't help me much. What is "that one object" referring to?

Comment
Add comment · Show 4 · 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 The_Toller · Oct 13, 2015 at 06:59 PM 0
Share

Oh, well this was actually exactly what I was looking for! But I must have done something weird since I know I tried something like that because it felt like the obvious solution but I didn't get it working correctly. I assumed that it was not possible to use the [ ] to retrieve an object from the list. That's why I did not even try it this time.

Hat off to you, sir! $$anonymous$$any thanks!

avatar image The_Toller · Oct 13, 2015 at 07:00 PM 0
Share

And no, I don't have a reference to the object anywhere else. :)

avatar image Dave-Carlile · Oct 13, 2015 at 07:04 PM 0
Share

Well, to be fair @Jessespike gave you the syntax first, so feel free to make that the accepted answer.

avatar image The_Toller Dave-Carlile · Oct 14, 2015 at 09:57 AM 0
Share

Yeah, I accidentally missed Spikes edit there. I keep your version as the accepted answer though since it was a bit more descriptive, in case someone else with the same issue would stumble upon this question. :)

avatar image
8

Answer by Jessespike · Oct 13, 2015 at 06:08 PM

Try using a Linq Query.

Edit: I guess I don't really understand the question. If you have 1 object in the list, you can still retrieve it by index.

 if(objects.Count() == 1)
 {
     GameObject obj = objects[0];
 }
Comment
Add comment · Show 3 · 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 The_Toller · Oct 13, 2015 at 06:24 PM 0
Share

Again, that seems like I need to know what that object is beforehand...

avatar image The_Toller · Oct 13, 2015 at 06:32 PM 0
Share

I have now updated my question with a code to explain better. :)

avatar image The_Toller · Oct 13, 2015 at 07:03 PM 0
Share

Exactly what I needed! $$anonymous$$any thanks! :D

avatar image
1

Answer by crestviewspirit247 · Mar 09, 2020 at 03:28 PM

All these are crazy complex if you just want to find a certain item using a random number. Let's say you wanna find a name in this list;

List names = new List(); names.Add("name1"); names.Add("name2"); names.Add("name3");

//You use a random int like so int finder = Random.Range(0, 4); //Then you just use this; nameDisplayString = names[finder];

Done!

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

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

33 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

Related Questions

Make Lists within a List 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Affect every object in array. 1 Answer

Unable to completely clear a list 2 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