• 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 Vicas · Jan 22, 2013 at 10:17 AM · gameobjectgetcomponentgeneric

A generic "Find GameObject From Path" method.

Hi.

I'm trying to make a small generic function to ease the exception handling and error messages when getting a gameObject from a path.

I've made this:

     public static class GameObjectHelper
     {
         public static Component GetGameObjectFromPath<T>(string path)
         {
             GameObject gameObject = GameObject.Find(path);
             Component c;
             if (gameObject != null)
             {
                 c = gameObject.GetComponent(typeof(T));
             }
             else
             {
                 throw new Exception("GameObject not found in " + path + ".");
             }
             
             return c;
         }
     }

So this works if I do something like this:

 SomeController someController = (SomeController) GameObjectHelper.GetGameObjectFromPath<SomeController>("/Menu/WorldNode/SceneObjects/SomeScene");

But I would like to do it without the type cast. Or generally know if I can make it better or more optimal.

Ideally it would be great if I could do just this:

 SomeController someController = GetGameObject("/the/path");

... with no casts or anything at all. But I am not sure that would be possible.

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

2 Replies

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

Answer by vbbartlett · Jan 22, 2013 at 10:28 PM

I don't believe that it is possible. C# is strongly typed and requires the type to be expressed, using a template is the best way as your example.

  t = (T)gameObject.GetComponent<T>();

which is pretty much what you are doing

... I would advise getting used to how C# works. You could look at #define to short cut it but that only works for one type...

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
avatar image Vicas · Mar 19, 2013 at 07:41 AM 0
Share

Since no one else gave input I must suppose your answer is correct. Until someone proves you wrong I accept this answer :-)

avatar image
0

Answer by Tarlius · Mar 19, 2013 at 09:13 AM

Assuming everything else is correct, you want to change

 public static Component GetGameObjectFromPath<T>(string path)

to:

 public static T GetGameObjectFromPath< T >(string path)

However, you will still have to declare what you expect the object to be with:

 GameObjectHelper.GetGameObjectFromPath<SomeController>("/Menu/WorldNode/SceneObjects/SomeScene");

but that will stop you needing a cast.

Also note that there is a generic version of GetComponent:

 GetComponent<SomeComponent>();

I believe you could avoid expressing the type if you refactor it to be more like:

 public static bool TryGetComponentFromPath<T>(string path, out T Component)

because that way you would be strictly defining "T" when you call the function. Also note that if you limit the declaration like

 public static bool TryGetComponentFromPath<T>(string path, out T Component) where T : class

you will be able to return null instead of throwing an exception (if preferred)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

how to access a float from one script in other script in different objects 3 Answers

How to Access Components in Children of GameObject? 1 Answer

Deactivate Script 1 Answer

Object reference not set to an instance of an object 3 Answers

Trouble accessing scripts (Boo) 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