Search Unity

Trouble using GetComponent<Image> in parent class of a button prefab.

Discussion in 'UGUI & TextMesh Pro' started by TheLurkingDev, Sep 6, 2014.

  1. TheLurkingDev

    TheLurkingDev

    Joined:
    Nov 16, 2009
    Posts:
    91
    I wasn't expecting this. Here is my code:
    Code (CSharp):
    1.  
    2.         int x = 50;
    3.         int count = 0;
    4.         foreach(Sprite thisSprite in GameTileSprites)
    5.         {              
    6.             TextureButton newButton = Instantiate(TextureButton, new Vector3(x, 68.8f, 0), Quaternion.identity) as TextureButton;
    7.             Image buttonImage = newButton.transform.GetComponent<Image>();
    8.             buttonImage.sprite = thisSprite;
    9.             newButton.transform.SetParent(panelTextureImages);
    10.             newButton.ElementIndex = count;
    11.             x += 90;
    12.             count += 1;
    13.         }
    If I instantiate as TextureButton, then I cannot obtain the Image component from the Button subclass. If I instantiate as a Button, then I cannot access the ElementIndex property of the superclass. I was under the impression that GetComponent would obtain any component within the complete gameobject. Tips? Solutions?
     
  2. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    Did you make sure to add a reference to the UnityEngine.UI?

    using UnityEngine.UI;
     
  3. TheLurkingDev

    TheLurkingDev

    Joined:
    Nov 16, 2009
    Posts:
    91
    While I had that problem about a week ago that is unfortunately not the solution to this problem. Thanks for the response though.
     
  4. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    533
    Maybe try parenting your object to a canvas before you try getting the image component? Btw, not related, but the following code:
    Code (CSharp):
    1. newButton.transform.GetComponent<Image>();
    can simply be:
    Code (CSharp):
    1. newButton.GetComponent<Image>();
    No need to dig into transform first. You can get any component from any component.
     
    TheLurkingDev likes this.
  5. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Yes, GetComponent should return any component on the GameObject in question.

    Things to check:
    • Make sure the Image component is actually on the same GO as the TextureButton component, and not on a child
    • Make sure you're instantiating the correct prefab
     
    TheLurkingDev likes this.
  6. TheLurkingDev

    TheLurkingDev

    Joined:
    Nov 16, 2009
    Posts:
    91
    Thanks again for your responses. I ended up solving it with the following code:

    Code (CSharp):
    1. void DisplayGameTileTextures()
    2.     {
    3.         int x = 50;
    4.         int count = 0;
    5.         foreach(Sprite thisSprite in GameTileSprites)
    6.         {      
    7.             TextureButton newTextureButton = Instantiate(NewTextureButton, new Vector3(x, 68.8f, 0), Quaternion.identity) as TextureButton;
    8.             newTextureButton.ElementIndex = count;
    9.             newTextureButton.transform.SetParent(panelTextureImages);
    10.             TextureButtons.Add(newTextureButton);
    11.  
    12.             Image buttonImage = newTextureButton.gameObject.GetComponent<Image>();
    13.             buttonImage.sprite = thisSprite;
    14.  
    15.             x += 90;
    16.             count += 1;
    17.         }
    18.     }