I have some code that creates objects with an Image component, in which I want to replace the Source Image with one of the default built-in sprites, through the code.


введите сюда описание изображения


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

public class PotionManager : MonoBehaviour {

    Image sourceImage;
    private float timer=25;
    static int COUNT_OF_ORDERS = 0;
    public Transform parent;
    public List<PotionAsset> potionList = new List<PotionAsset>();
    private float posX,posY,posZ;

    // Use this for initialization
    void Start () {


    // Update is called once per frame
    void Update()
        timer += Time.deltaTime;
        if (timer >= 30)

    void CreateOrderHolder()
        posX = 160 * COUNT_OF_ORDERS + 80;
        posY = -70;
        posZ = 0;


        GameObject temp = new GameObject("Potion Order " + COUNT_OF_ORDERS, typeof(RectTransform));
        temp.transform.localPosition = new Vector3(posX, posY, posZ);
        temp.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
        temp.GetComponent<RectTransform>().anchorMax = new Vector2(0, 1);
        temp.GetComponent<RectTransform>().anchorMin = new Vector2(0, 1);


        timer = 0;

    void CreatePotionHolder(Transform parent)
        posX = 0;
        posY = 13.5f;
        posZ = 0;

        GameObject temp = new GameObject("PotionHolder", typeof(RectTransform), typeof(Image));

        temp.transform.localPosition = new Vector3(posX, posY, posZ);
        temp.transform.localScale = new Vector3(1, 1, 1);
        temp.GetComponent<RectTransform>().sizeDelta = new Vector2(100,50);

        sourceImage = temp.GetComponent<Image>();
        sourceImage.sprite = Resources.Load<Sprite>("Resources/unity_builtin_extra/UISprite");


In the end, sourceImage.sprite returns null. I tried to add at the end of the file path the file extension like png, psd, but this did not help.

  • \$\begingroup\$ Resources.Load returns null if it can not find the asset under the provided filename. Where exactly is the file located? Also, what kind of file is it? Resources.Load<Sprite> will require an Unity sprite asset at that position. Not an image asset or a .psd file. \$\endgroup\$
    – Philipp
    Commented Jul 24, 2019 at 10:43
  • 1
    \$\begingroup\$ Also note that use of a Resources folder is deprecated. The new recommended way of loading assets at runtime is Addressable Assets. \$\endgroup\$
    – Philipp
    Commented Jul 24, 2019 at 10:47
  • 3
    \$\begingroup\$ Have you considered attaching this sprite to your script as a public/serialized member variable, so you don't need to go hunting for it at all? Doing this makes the dependency visible to Unity, so it can ensure the sprite is loaded and ready to use when your script is instantiated. \$\endgroup\$
    – DMGregory
    Commented Jul 24, 2019 at 11:54
  • \$\begingroup\$ @Philipp I know, that Resources.Load can't find asset under provided filename. That's why I asking my question. Where I can find that sprite(and how to use it properly). It's into my project folder, into my unity folder? Where?) \$\endgroup\$ Commented Jul 24, 2019 at 12:24
  • \$\begingroup\$ @DMGregory Yeah, i was thinking about your method, but I wanna exactly same sprite. That's why I don't know where I can find it? In my project folder, my unity folder?) \$\endgroup\$ Commented Jul 24, 2019 at 12:27

1 Answer 1


You can access the built in Extra Resources in the editor. I believe if you make a public reference to the UI sprite it will create a replica to use for runtime somewhere along the way.

Otherwise the path to the sprite is 'UI/Skin/UISprite.psd'


sourceImage = temp.GetComponent<Image>();
sourceImage.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd");

I'd suggest using Sprite.Create(); during runtime and you can create a texture for it also so you have no need to load their default one.

  • \$\begingroup\$ note that you could also load and make a copy of the default UISprite in the editor and then place the 'copy' as your own resource to load whenever for later use in your project. \$\endgroup\$ Commented Jul 24, 2019 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .