Code linking png image to Image Component

I have a game object that is dynamically created by a script attached to my Main Camera, but I can’t get the “A.png” to link into the Image Sprite reference. Below is everything I can provide. As you can see in the first screenshot, the “Package” object is created from the script and has the Image component attached, but it still does not find the image A.png that is located in my Assets > Resources > Images folder. I have no idea what I’m missing, any suggestions?
Code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class Placement : MonoBehaviour
{
    public GameObject buildLetter;
    public Sprite aSprite;


    void Start ()
    {
        buildLetter = new GameObject("Package");
        buildLetter.transform.position = new Vector3(0, 0, 0);
        buildLetter.AddComponent(typeof(Image));
        aSprite = Resources.Load<Sprite>("Images/A.png");
        buildLetter.GetComponent<Image>().sprite = aSprite;

    }

    void Update ()
    {

    }
}


Here is a direct link to the images if they aren’t big enough:


At least part of the problem is the .png… when you are calling Resources.Load(), don’t include file extensions.

Kurt, Thanks for the reply. That is closer in the right direction because the Image Source now says “A” but nothing displays in my Game window. Do you recommend an alternative method to call the image? Maybe there’s a better solution to load the A.png? If not, what am I missing so the image displays?

A couple of thoughts: make sure the target SpriteRenderer is the expected size. You can do this by just dragging it into the sprite in inspector and seeing if it seems reasonable, then doing it script. Perhaps some scale or pixels-per-inch is wrong or something like that. I’ve not messed with sprites for a few months now so I forget the ways it can fail to show up.

Try also paring down your scene to just a single object that your script loads the Sprite onto, and make sure it is in front of the camera, etc. Winnow out the possible ways of failure, etc.

Tired your suggestion and it still didn’t display, I think part of my problem is trying to load it with the legacy GUI system, I would prefer using the new GUI 4.6 system to dynamically create the 4.6 button with a script. Because I can see this button displayed correctly in my game (4.6 button). Either that or have it created by using instantiate or something similar. Could the new 4.6 button get created dynamically or should I try to instantiate a button that works?

Sorry for my rookie questions.

I did some sleuthing on this to try and understand the new UI a bit better.

Turns out the Image component requires a Canvas as its parent. Not sure if you had that in your original scene.

To get this loading working in a minimal scene:

  • Ctrl-N for new scene
  • attach this script to a blank game object
  • use GameObject->UI->Canvas to create a canvas

Run. Depending on screen resolution, your “A” should be visible.

Kurt

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Placement : MonoBehaviour
{
    public GameObject buildLetter;
    public Sprite aSprite;
  
    void Start ()
    {
        buildLetter = new GameObject("Package");
        buildLetter.transform.position = new Vector3( 111, 111, 0);

        Canvas canvas = FindObjectOfType<Canvas>();
        // This will produce a Canvas but the scale will not at all
        // be the same as if you used the Unity3D menus to create it.
        if (canvas == null)
        {
            canvas = new GameObject( "Canvas").AddComponent<Canvas>();
        }
        buildLetter.transform.parent = canvas.transform;

        Image image = buildLetter.AddComponent<Image>();
        aSprite = Resources.Load<Sprite>("A");        // was "Images/A.png"
        image.sprite = aSprite;
    }
}

Kurt,

Thank you very much for sending this. Sorry I’ve been away for two weeks. I’ve been reading books on C# and Unity trying to catch up! Still reading. I just tried the example you sent and I get an empty white box. It does create two new game objects children of “canvas”, but they are just white boxes. When I click on them the Image component is attached but the Image source field is empty.

It appears the png file can’t be found. Any idea on what is causing this? My images are located in "Resources > Images.

Thanks again for this!

Nevermind. I had to remove the “Images” folder. Now it works like a charm. I did not know the Images folder could not exist inside the root of Resources.