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 MarlowUK · May 13, 2014 at 02:23 PM · gameobjectprefabgetcomponentinchildren

Sporadic Failure of GetComponentInChildren

After about 2 months of learning Unity programming in 2D, I’ve finally hit a problem that I can’t solve by looking through the forums and finding a similar problem.

I have built simple target game that Instantiates prefabs to which I’ve attached sprites, colliders and scripts.

In the Start method of my game logic, I’m grabbing a reference to the target GameObject and, specifically, the script attached to one of its children.

 TargetCode target_script = GameObject.Find(game_object_target).GetComponentInChildren<TargetCode> ();

My problem is that about 19 times out of 20, this works fine. The game plays without a problem.

However, occasionally, the above line doesn’t find the script and returns null.

I can hack a solution by adding a later check for the target script when needed in the Update method but I want to know why this problem is happening in the first place.

The only unusual thing I've done is to pass the name of the GameObject through a string but a Debug.Log check shows that this is defined correctly and this doesn’t explain the sporadic nature of the bug. I’ve also added a Debug.Break() to the code and I can see that the target exists and the script should be findable (as it is findable later in the Update).

Are they are known problems with GetComponentInChildren? I wondered about the order of execution of Start methods across Instantiated prefabs but I’ve not come across any similar problems or solutions to this problem.

Many thanks in advance.

Comment
Add comment · Show 2
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 Pawscal · May 13, 2014 at 04:46 PM 0
Share

Is it possible that your GameObject is not finished created? have you tried doing it on two separate lines of codes? GameObject myObject = GameObject.Find(game_object_target); TargetCode targetScript = myObject.GetComponentInChildren ();

avatar image ThePunisher · May 13, 2014 at 06:32 PM 0
Share

A few questions that will surface the problem.

Are you adding the component (TargetCode) dynamically? If so, are you adding it in Start?

Is the object you are searching already on the scene or does it get created during runtime? If so, does it get created in Start?

Something you have to always remember is that Awake is used to initialize your own component so that it can be ready for use. And start is used to communicate/initialize between components or GameObjects.

0 Replies

· Add your reply
  • Sort: 

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

Instantiated objects always at position 0,0,0 0 Answers

A node in a childnode? 1 Answer

Is it possible to edit the sub-sub-GameObjects of a prefab in the assets window? 1 Answer

How to add a prefab back to a gameobject that is missing one? 5 Answers

How does Unity retain UnityEngine.Object references? 2 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