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 MindGem · Sep 13, 2021 at 06:19 PM · null

GetComponent() returns null

I can't figure out what Im doing wrong here.

I'm trying to reach an image inside a gameobect on the scene. I know the image exist.

  Debug.Log(Container.transform.GetChild(i).Find("Fill - Top").transform.name);

Gives me "Fill - Top" which is the name of the gameobject im trying to reach, so that should indicated that it exist right. and in the hirarchy at runtime I see the gameobject is there with the image inside.

but this one gives me null

 Debug.Log(Container.transform.GetChild(i).Find("Fill - Top").transform.GetComponent<Image>());

what am i doing wrong?

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
0
Best Answer

Answer by Bunny83 · Sep 13, 2021 at 06:57 PM

Well, if GetComponent returns null there's only one possible reason for this: The object you're calling GetComponent on does not have that component.


To narrow down the issues further, lets break it down into smaller pieces. First of all, make sure the object you're dealing with is actually the right one. For this you can pass that object as second "context" parameter to a Debug.Log statement. When you do this, clicking the log message in the console will "ping" / highlight that specific object in the scene or project panel so you know exactly what object you're dealing with.


Next you should make sure you use the "correct" Image class. There are several Image classes and you could even define your own class. We don't know which one you're looking for, but it may be the one in the UnityEngine.UI namespace? Maybe you used the wrong namespace?


So if you're not sure about the Image type you're using, in VisualStudio you can simply hover over the type and it should tell you in the hint that pops up the full classname including the namespace.


Though you can also simply print the full name of the type. So try this in your code:

 Transform obj = Container.transform.GetChild(i).Find("Fill - Top");
 Debug.Log("Finding type: " + typeof(Image).FullName + " on object" + obj.name, obj);

This should answer both questions at once. You would see the full name of the Image class and when you click on the log message it should tell you the exact object.

Comment
Add comment · Show 3 · 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 MindGem · Sep 13, 2021 at 07:11 PM 0
Share

I manage to get it but it seems redundant and way too elaborate. must be a simpler way. it's a long and illegible line.

Container.transform.GetChild(i).Find(elementNames[i]).gameObject.transform.GetChild(i).gameObject.GetComponent().color = allColors[i];

avatar image Bunny83 MindGem · Sep 13, 2021 at 08:09 PM 1
Share

Why do so many people always add a .gameObject or a .transform everywhere, just in case? GetChild as well as Find both return a Transform component. So using ".transform" on a transform reference is completely pointless. The ".transform" property is just a shortcut for GetComponent<Transform>().

You should generally avoid such long chains because if anywhere in that chain is an error, you can't tell which part fails. Break up such long chains into seperate lines with proper temp variables. This automatically documents what the code is actually doing, since you can name those variables properly. Also if a null reference exception happens at any stage, you know the line number and exactly at which point it fails.


We don't know what type your Container variable has. If it's a Transform reference, you would not need the first ".transform" either. Anyways you can reduce your single line to

 Image img = Container.transform.GetChild(i).Find(elementNames[i]).GetChild(i).GetComponent<Image>()
 img.color = allColors[i];

Though as I said, it's better to break the code up into individual parts like

 var child = Container.transform.GetChild(i);
 var subElement = child.Find(elementNames[i]);
 var elementChild = subElement.GetChild(i);
 var img = elementChild.GetComponent<Image>();

However your code seems very strange. Why do you have so many sub objects? Also it seems very weird that the child index at the first level (your "i" value) matches the child index inside the nested object. This looks like a very strange setup. You should always try to avoid manually searching through code if possible. Try linking relevant objects in the inspector by exposing a Transform (or in your case an Image variable) that you can assign in the inspector. Of course that's not always possible.


Note that if there is only one Image component in a whole object sub tree, you can simply use "GetComponentInChildren" instead of GetComponent. It returns the first component it can find on the object you call this method on or any of its child objects. So often times it's not necessary to manually walk down the hierarchy. You can also use your own scripts as a kind of "marker". So you attach your script to a nested object and call GetComponentInChildren to get that nested object (given there is only one such object in the tree you're searching through).

avatar image MindGem Bunny83 · Sep 13, 2021 at 08:15 PM 0
Share

I don't know why others do it but I do it because it's still abstract for me how it works in Unity. I'm learning.

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

125 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

Related Questions

how can I check if an object is null? 4 Answers

what is null 1 Answer

XMLDocument GetElementByID returning null 1 Answer

Confused as to why I'm getting a NullReference Exception 1 Answer

Destroying an object 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