- Home /
How to Find a Game Object from a List and assign?
I Just stomped on how to achieve this I have been searching for hours , so what im wandering how can I call a game object from a list using a string? like for example this is just a new Public Game object list, but I want to find a gameobject in the list but return it as a game object so i can put it in another variable in this senario into the GameObject PrimaryWeapon
//I want to find a GameObject called "Gun" inside this list
//and put it in the Public gameObject
public List<GameObject> Weapons = new List<GameObject>();
public GameObject PrimaryWeapon;
....so far i tried to return it like this but no luck
GameObject responseP = Weapons.Find("Gun".Equals);
PrimaryWeapon = responceP;
Answer by DocteurCox · Jul 30, 2013 at 01:48 PM
You could use Linq. Import System.Linq in your script and try this :
PrimaryWeapon = myObjList.Where(x => x.name == yourname).SingleOrDefault();
This should work, though I'm not entirely sure this is what you are looking for. SingleOrDefault() returns a GameObject if it could find something that matches or return null. So don't forget to check your object after that ;)
nope it didnt work...:( I honestly dont know how to do this.....@___@ ...
What's the output ? Do you get an error or something ?
All the ways described here works pretty well. So i guess you simply don't have a gameobject with the name "Gun" in your list, it's simply as that.
just do something like this and you will see what GameObjects are in your List:
Debug.Log("Enumerating Weapons:");
foreach(var W in Weapons)
Debug.Log("Name: " + W.name + " (Length: " + W.name.Length + ")");
Debug.Log("done");
Use this ans tell use what you got. Also check the "Length" that is returned, maybe your GameObject name has spaces somewhere.
Answer by nixcs2512 · Jul 30, 2013 at 01:45 PM
Simply use that:
GameObject responseP = Weapons.Find(obj=>obj.name=="Gun");
PrimaryWeapon = responceP;
Remember that the responseP is the first GameObject named "Gun" in Weapons list so it might not work well if you have two or more "Gun".
hmmm does it matter if im calling it from a string? ins$$anonymous$$d of gun? likein GameObject responseP = Weapons.Find(obj=>obj.name==CurrentWeapon);
It doesnt matter if CurrentWeapon is a string. Try to place a Debug.Log(responseP) in between to see if it works.
Answer by Xtro · Jul 30, 2013 at 02:15 PM
I don't prefer to use Linq in Unity.
You must write a foreach loop to go through each item in it, and you should test each item's name with your string...
something like that: (C# script)
foreach(var Item in Weapons){
if (Item.name == "Gun"){
PrimaryWeapon = Item;
break;
}
}
I don't see any reason not to use Linq in Unity. It provides a simple and generic way to access data. Well of course, for such a simple example, Linq might be a bit of an overkill but there are a lot of things Linq does and you'd waste a lot of time to reimplement.
I love to use Linq in my .NET projects but I saw on the Internet that, there were some people complaining about running Linq on mobile platforms. Some crashes etc... I just wanted to stay away from it in my game development experience.
It's not a database application anyway.
I agree with @DocteurCox, i just used Linq a little time so i dont know much but at least with me, code written with Linq is shorter and easier to edit than old way using for/foreach loop.
ok i tested it and no errors ..it worked ..now there has to be something wrong in the way im calling this..
Answer by TonyLi · Jul 30, 2013 at 01:21 PM
Try a delegate:
GameObject responseP = Weapons.Find(w => string.Equals(w.name, "Gun"));
Or perhaps you'd be better off using a Dictionary that uses the GameObjects' names as keys.
Also, when you get a reference (e.g., using Find or GetComponent) always check the result to make sure it's not null.
BTW, in your code snippet, you assign responseP and then immediately reassign responceP (sic?) = PrimaryWeapon, which makes the first assignment have no meaning.
yh delegates didnt work :( .... hmmm what do you mean by a Dictionary that uses names as keys ?? like whats the logic for that method ?? is it like an array or list ?
ohhh no i just cut it short the actual script i quite long and i don't what to bore people that can potentially help me so i just cut out the necessary parts ^.^
Answer by junedmmn · Dec 02, 2018 at 07:59 PM
It's easy, just use list.Find(x => x.name == "stringNameOfObjectToFind");
and then Instantiate
GameObject GameObject = Instantiate(list.Find(x => x.name == "ObjectNameString")) as GameObject;
Your answer
