Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 kowbonez · May 29, 2019 at 11:17 PM · prefabgetcomponentinchildren

Cannot GetComponentInChild of a Prefab

I am trying to create a UI panel with a Text object as its child into a prefab. Every time the function is called it's supposed to produce this prefab. I am getting this error:

NullReferenceException: Object reference not set to an instance of an object

The function looks like this:

 private void CreateLines() {
             foreach (var line in lines) {
                 message = new GameObject("New Message");
 
                 message.transform.SetParent(content, false);
                 messageText = message.GetComponentInChildren<Text>(true);
                 messageText.text = line.text;    
             }
         }


In my searching, it looks like this is not possible currently. If this is the case, what is a good work around?

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 Bunny83 · May 30, 2019 at 12:07 AM

You mixed up terms that don't belong here. You don't have any prefabs involved here. All you do is creating a new empty gameobject each for loop iteration. Of course a new empty gameobject doesn't have any components except the Tranform component.


GetComponent / GetComponentInChildren will search for an existing component on the given object and return the reference to it if one was found. As i said an empty gameobject doesn't have any components, so obviously GetComponentInChildren will return null. There's also no point in using GetComponentInChildren over GetComponent in this case since a new gameobject doesn't have any children and you don't attach any.


It's not really clear what you want to do. I see two possible scenarios:

  • You actually created a prefab in the project that should represent a message. This prefab may have a Text component on a child object. If that's the case you don't want to use new GameObject but instead use message = Instantiate(yourPrefabReference); to create an instance of your prefab.

  • If you don't have a prefab setup in your project and you actually want to create the object on the fly procedurally, you should use message .AddComponent<Text>() to add a new Text component to your message object. Though keep in mind that you have to initialize everything manually


In many cases it's easier to setup a prefab in the editor and instantiate it at runtime.

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 kowbonez · May 30, 2019 at 12:55 AM 0
Share

You actually created a prefab in the project that should represent a message. This prefab may have a Text component on a child object. If that's the case you don't want to use new GameObject but ins$$anonymous$$d use message = Instantiate(yourPrefabReference); to create an instance of your prefab.

Thank you, this works now.

 private void CreateLines() {
             foreach (var line in lines) {
                 message = Instantiate(message);
 
                 message.transform.SetParent(content, false);
                 messageText = message.GetComponent<Text>();
                 messageText.text = line.text;
             }
         }


avatar image Bunny83 kowbonez · May 30, 2019 at 01:26 AM 0
Share

Note that if "message" is your prefab reference you shouldn't replace it with an instance of the prefab. The second call of Instantiate would clone not the prefab but the object already in the scene. This may not be an issue in your case but can cause several issues in the long run. So if message is your prefab reference, you should do something like this:

 foreach (var line in lines) {
     var tmp$$anonymous$$sg = Instantiate(message);
     tmp$$anonymous$$sg.transform.SetParent(content, false);
     var msgText = tmp$$anonymous$$sg.GetComponent<Text>();
     msgText.text = line.text;
 }

As a general rule: Don't use member variables as temporary variables. Local variables are faster to access and cause less trouble with the outer scope.


If you have trouble to distinguish object instances from your prefabs it might be a good idea to rename your "message" variable into "messagePrefab".

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

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

Related Questions

How to get script from Object with (Clone)'s script? 2 Answers

Problem with GetComponentinChildren and Instantiate 0 Answers

Counter for sprite under script from GameObject prefab 0 Answers

Sporadic Failure of GetComponentInChildren 0 Answers

GetComponentInChildren 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