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
0
Question by xenix2012 · Apr 23, 2014 at 08:13 AM · c#findfind-gameobject

Finding GameObject without using the full name

Hi, I have problem with this one:

GameObject.Find("MyGameObject")

finds only GameObjects with this specific name. I have to find GameObjects with with a name that contains a a fragment of string, for example:

GameObject.Find("Car")

finds Car1, Car2 etc. Any ideas? :)

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

1 Reply

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

Answer by robertbu · Apr 23, 2014 at 08:24 AM

If you are not using your tags for anything else, then the easiest solution would be to tag all of your cars with a 'Car' tag. Then you would not need a string search. You could then use GameObject.FindGameObjectsWithTag() to get all the cars. If you really need the substring search, you could use GameObject.FindObjectsOfType() to get a list of all the game objects. Then you could cycle through the list using any string comparison that makes sense to find your objects. Note that both FindGameObjectsWithTag() and FindObjectsOfType() only return active game objects and that FindObjectsOfType() is slow.

As a way of creating a more compact search if you need to cycle through all the game objects, take a look at LinQ:

http://unitygems.com/linq-1-time-linq/

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 xenix2012 · Apr 23, 2014 at 08:29 AM 0
Share

I belive that idea with tags should work, thanks! :)

avatar image robertbu · Apr 23, 2014 at 08:41 AM 0
Share

Tags is the most efficient solution. I was just playing with LinQ with your criteria. Since I use LinQ infrequently, I always have to stumble around a bit, but here is a short script:

 #pragma strict
 import System.Linq;
 
 function Start() {
     var carObjects = GameObject.FindObjectsOfType(GameObject)
                     .Where (function (g) g.name.Contains("Car"));
          
     for (var car : GameObject in carObjects)
         Debug.Log(car.name);
 }

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

20 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

Related Questions

C# The call is ambiguous between the following methods or properties 1 Answer

Distribute terrain in zones 3 Answers

Finding inactive game objects in a pool 1 Answer

Can't change variable in script cia "GameObject.Find" (C#) 1 Answer

Multiple Cars not working 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