- Home /
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? :)
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.
@$$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
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?
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!
And no, I don't have a reference to the object anywhere else. :)
Well, to be fair @Jessespike gave you the syntax first, so feel free to make that the accepted answer.
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. :)
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];
}
Again, that seems like I need to know what that object is beforehand...
I have now updated my question with a code to explain better. :)
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!
Your answer
Follow this Question
Related Questions
Keep list of GameObjects between scenes 2 Answers
Error when transferring items from one list to another 1 Answer
List not adding 1 Answer
Make Lists within a List 0 Answers
Multiple Cars not working 1 Answer