Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by darkcookie · Jul 30, 2013 at 01:16 PM · gameobjectlistreturn

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;

   
Comment
Add comment
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

5 Replies

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

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 ;)

Comment
Add comment · Show 7 · 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 darkcookie · Jul 30, 2013 at 01:58 PM 0
Share

ok i imported the system im testing

avatar image darkcookie · Jul 30, 2013 at 02:07 PM 0
Share

nope it didnt work...:( I honestly dont know how to do this.....@___@ ...

avatar image DocteurCox · Jul 30, 2013 at 02:11 PM 0
Share

What's the output ? Do you get an error or something ?

avatar image darkcookie · Jul 30, 2013 at 02:17 PM 0
Share

WAIT OUTPUT? debug.log?

avatar image Bunny83 · Jul 30, 2013 at 02:17 PM 0
Share

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.

Show more comments
avatar image
4

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

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 darkcookie · Jul 30, 2013 at 01:54 PM 0
Share

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);

avatar image darkcookie · Jul 30, 2013 at 01:55 PM 0
Share

because it didnt work:(

avatar image nixcs2512 · Jul 30, 2013 at 02:09 PM 0
Share

It doesnt matter if CurrentWeapon is a string. Try to place a Debug.Log(responseP) in between to see if it works.

avatar image
4

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;
     } 
 }
Comment
Add comment · Show 6 · 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 darkcookie · Jul 30, 2013 at 02:18 PM 0
Share

ok ill test it

avatar image DocteurCox · Jul 30, 2013 at 02:20 PM 1
Share

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.

avatar image Xtro · Jul 30, 2013 at 02:24 PM 1
Share

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.

avatar image nixcs2512 · Jul 30, 2013 at 02:28 PM 1
Share

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.

avatar image darkcookie · Jul 30, 2013 at 02:29 PM 0
Share

ok i tested it and no errors ..it worked ..now there has to be something wrong in the way im calling this..

Show more comments
avatar image
2

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.

Comment
Add comment · Show 2 · 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 darkcookie · Jul 30, 2013 at 01:29 PM 0
Share

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 ?

avatar image darkcookie · Jul 30, 2013 at 01:32 PM 0
Share

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

avatar image
0

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;
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

22 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

Related Questions

A node in a childnode? 1 Answer

use list of gameobjects transform in raycast 0 Answers

Enabled all gameobjects in array 1 Answer

In a tile-based game, best way to select multiple tiles via dragging? 1 Answer

Getting a List of Gameobjects in a collider 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