• 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 crezax · Mar 28, 2013 at 03:53 PM · gameobject

Accessing game objects, that are part of another game object

Hey guys

I am quite new to Unity and after playing around with some stuff, I have a small problem. Let's say I create an empty GameObject and add some objects to it. For example I can have a GameObject, or Prefab called PlayerCharacter, which contains 3 objects:

  • Model

  • SpellSpawnpoint

  • Camera

Now I add a script to PlayerCharacter, for example one creating spells. I need to access SpellSpawnpoint object to instantiate spell, but... how?

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

3 Replies

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

Answer by psycocrusher · Mar 30, 2013 at 01:33 AM

When the object is a child you can use transform.Find("") instead of gameObject.Find(""), it will look under the group automatically.

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
avatar image
1

Answer by Yokimato · Mar 28, 2013 at 03:58 PM

Welcome to Unity.

So you've hit a part that, for whatever reason, is not built in! It sucks, I know. Here's how you can work around it thoughL

 GameObject player = GameObject.Find("PlayerCharacter");
 GameObject spellSpawnPoint = null;
 
 foreach(Transform subTransform in player.transform) {
    if(subTransform.gameObject.name == "SpellSpawnpoint") {
       spellSpawnPoint = subTransform.gameObject;
       break;
    }
 }

It's not pretty by any means, but it works.

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
avatar image
1

Answer by robertbu · Mar 28, 2013 at 03:57 PM

How about:

GameObject.GetComponentInChildren()

Comment
Add comment · Show 4 · 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 Yokimato · Mar 28, 2013 at 03:58 PM 1
Share

He's looking for the sub gameObject though, not the component. Although, my guess is that he's looking to ultimately end up at some component ;)

avatar image robertbu · Mar 28, 2013 at 04:02 PM 0
Share

If he just wants to find the game object, how about:

Transform.Find()

avatar image Yokimato · Mar 28, 2013 at 04:11 PM 1
Share

Which is another solution, but if I had to guess at the implementation of that method, it's searches the entire scene graph for it; albeit cleaner than $$anonymous$$e, I'd be nervous about performance depending how the frequency in which it's called.

avatar image robertbu · Mar 28, 2013 at 06:10 PM 0
Share

It does not search the entire scene. It's an instance, not a class method, so it only searches from the given transform.

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

12 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

line render from object to object 1 Answer

How to make gameobject follow mouse pointer that's on rotated orthographic camera? 0 Answers

Ray Casting - Trigger function 1 Answer

Any way to lock an object from changes in the editor? 4 Answers


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