Reputation: 305
In the app I'm building in Unity3d, I want to be able to have the user choose a background image for the main panel. At the moment, I'm getting null when I debug out the variables. If someone could point me in the right direction.
public Image Background;
public Sprite theImage;
// Use this for initialization
void Start ()
{
theImage = Resources.Load<Sprite>("Sprites/sf1");
Debug.Log(theImage);
Background.GetComponent<Image>().sprite=theImage;
Debug.Log(theImage);
Thanks!
Upvotes: 2
Views: 1563
Reputation: 20028
One thing to remember is that there is no such thing as a panel. "But I just created one" I hear you say. Sure, Unity makes you think you did. But if you look at the gameobject it created, you'll see it's nothing more than a GameObject with a RectTransform component and and Image component.
So if you were to add your own component to this Panel, all you'd need to do is something along the lines of
Image image = GetComponent<Image>();
image.sprite = mySprite;
So your code is almost there, if not for the unassigned Background
image.
Upvotes: 2