Search Unity

GetComponent<Image> returning null?

Discussion in 'UGUI & TextMesh Pro' started by MV10, Jul 1, 2016.

  1. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    I'm almost embarassed to ask about this, it has to be something simple, but I see no reason it shouldn't work. In the hierarchy I have a Canvas which parents a background Image, and that in turn parents the target Image which has this script on it:

    Code (csharp):
    1. private Image img;
    2.  
    3. void Start()
    4. {
    5.     if(img == null) img = GetComponent<Image>();
    6.     for(int i = 0; i < sprites.Length; i++) { ... }
    7. }
    The loop details don't matter (it finds a sprite by name and assigns it to img.Sprite), and that is literally the entire script.

    The problem is that GetComponent always returns null.

    Intellisense shows that <Image> is of the correct UnityEngine.UI.Image type (I don't have something else called Image that is hiding the UI version). I worked around it by creating a public Image variable for Inspector assignment, but that's pretty goofy considering the script is on the Image itself already.
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @MV10: Hi,

    GetComponent is only ever going to get component in question from GameObject itself the script is attached to.

    To get any (first) component of type from children of transform use:

    Code (csharp):
    1.  
    2. GetComponentInChildren<Image>();
    3.  
    To get all *components* of type from children use:

    Code (csharp):
    1.  
    2. public Image[] images;
    3.  
    4. images = GetComponents<Image>();
    5.  
    6. foreach (Image img in images)
    7. {
    8. // Do stuff
    9. }
    10.  
     
  3. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    I understand that, and that is the probem. The script is on the GameObject which has an Image component that I am trying to retrieve. I am not looking for components on any child objects (the target object has no children) and I am not looking for all Image components.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @MV10 - well, your code works here...
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I didn't see anything wrong with the code, so I tested it myself and it worked fine as well.
    @MV10 When you run this code, you get a nullReferenceError when trying to do something with the image?

    If the variable is public and has no value assigned to it in the inspector(so it says none) and then you run this code, does it populate it in the inspector while in play mode?
     
  6. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    @MV10 When you run this code, you get a nullReferenceError when trying to do something with the image?

    If the variable is public and has no value assigned to it in the inspector(so it says none) and then you run this code, does it populate it in the inspector while in play mode?[/QUOTE]

    Yes that's what happened. I added Debug.Log(img == null) after it and got True, same thing as your Inspector suggestion. I restarted Unity and VS several times yesterday before giving up and going the public Inspector route.

    But today it... works fine... o_O

    In any case, I appreciate the attempts to assist!
     
  7. nebosite

    nebosite

    Joined:
    Mar 13, 2013
    Posts:
    10
    This is happening for me as well. Suddenly code that was working now does not. The image is on the same object that is running the code to get the image component. I've tried rebuilding the project, closing and opening the IDE and these don't help. Now I will reboot my computer.
     
  8. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    is your game object active?
     
  9. Kevinwgm

    Kevinwgm

    Joined:
    Feb 11, 2022
    Posts:
    2
    I have a question on unity
    NullReferenceException: Object reference not set to an instance of an object
    BarScript.HandleBar () (at Assets/scripts/BarScript.cs:38)
    BarScript.Update () (at Assets/scripts/BarScript.cs:33)
    I dont no how to fix it .
     

    Attached Files: