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?
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.