G
G
Georgy Makovetsky2019-02-24 21:01:48
C++ / C#
Georgy Makovetsky, 2019-02-24 21:01:48

Why does Resources.Load() return null?

The code:

Sprite pistolSprite;
  GameObject pistol;
  public string pistolPath = "Sprites/Pistols/pistol";
  public int damage;

  void Start () {

    pistolSprite = (Sprite)(Resources.Load (pistolPath, typeof(Sprite)));	
    if (pistolSprite == null)
      Debug.Log ("Pistol sprite wasnt found");
    pistol = new GameObject("pistol");
    pistol.AddComponent<SpriteRenderer>().sprite = pistolSprite;

  }

  void Update () {
      
      pistol.transform.position = new Vector3 (transform.position.x, transform.position.y, 0f);
      pistol.transform.rotation = transform.rotation;
    
  }

As expected, Debug.Log() produces what is written. The pistol.png file is located in the Assets/Resources/Sprites/Pistols/pistol directory.
I don’t know the complexity of the question, so I put the average level

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2019-02-26
@YtnEtc

pistolSprite =  Resources.Load <Sprite> (pistolPath);	
//или если несколько внутри то 
Sprite[] pistolSprites =  Resources.Load <Sprite> (pistolPath);

that's how it should work. in your version, it probably found exactly the texture, and then tried to convert it to a sprite because of the typeof (Sprite)

D
Dmitry Bashinsky, 2019-02-25
@BashkaMen

Maybe you should add .png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question