Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by mrlovepump · Nov 13, 2013 at 09:32 PM · 2dspritesceneresources.loadunity 4.3

Resources.Load sprite returning null (4.3)

Evening all,

I am attempting to programmatically populate my scene based on data in an XML file. I have parsed and created an object model from the XML no problem.

Now I am trying to create and add sprites to the scene. The sprite already exists within my assets folder.

I am doing the following where item.name matches the name of my sprite in unity.

                 var gameObject = (GameObject)Resources.Load(item.Name) as GameObject;
 
                 if(gameObject == null) Debug.LogError("item GameObject is null");
 
                 Instantiate(gameObject, new Vector3((float)item.UnityPositionX, (float)item.UnityPositionY, layer.Depth), new Quaternion(0,0,0,0));

However gameObject is always null.

Any suggestions or pointers would be much appreciated.

Thanks

Edit -

Through trial and error I got the following code to programmatic ally put a new 2d sprite type into the scene at runtime.

         var gameObject = new GameObject ();
         gameObject.transform.position = new Vector3 (0, 0, 0);
 
         gameObject.AddComponent ("SpriteRenderer");
 
         var renderer = (SpriteRenderer)gameObject.GetComponent ("SpriteRenderer");
 
         renderer.sprite = Resources.Load<Sprite>("Grass50x50");
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ArkaneX · Nov 13, 2013 at 09:48 PM 0
Share
  1. If you do a cast operation using parentheses, then converting with 'as' keyword is not required.

  2. Path passed to Resources.Load must be relative to Resources folder, and file extension must be omitted. Does your item.Name follows these rules?

avatar image liszto · Nov 14, 2013 at 06:54 AM 0
Share

Yeah I'm really not convinced by your item.name maybe the name in the prenthesis it's not good ? It's exactly the same name as one of one of your prefab in Resources folder ?

avatar image mrlovepump · Nov 14, 2013 at 08:02 AM 0
Share

ArkaneX, thanks yeah I understand with the casting. I am an experienced C# dev and that was taken from an example online. I removed it originally then added it back out of desperation.

Liszto, It is indeed exactly the same indeed.

Both,I will put together an isolated example and update the question with the resource name as a string etc. Wont be long.

avatar image Eric5h5 · Nov 14, 2013 at 08:22 AM 2
Share

A Sprite isn't a GameObject, if that's what you're trying to do. Also I'd recommend not putting sprites in Resources because they won't be able to generate atlases.

avatar image mrlovepump · Nov 14, 2013 at 08:39 AM 0
Share

I have now tested in isolation and can get a sprite to load into gameobject so its obviously something else, thanks all. Scrub that it doesnt seem to be working. Think I need to get my head down and get a better feel for programmatic game object creation.

Eric - Even in 4.3? It was my understanding that it did? It would be great if you could expand further, would be a really appreciated.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
16
Best Answer

Answer by mrlovepump · Nov 14, 2013 at 02:42 PM

OK, I have got the sprite object loading in correctly. I can only assume some of the examples out there are old and the objects dont work the way they once did.

The following loads a sprite from resources just fine.

 var sprite = Resources.Load<Sprite>("Grass50x50");

Now the next stage is to use that to build up, in code a 2D sprite based game object in the same way it would be created using the Unity editor.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mattssonon · Nov 14, 2013 at 02:49 PM 0
Share

Please accept your answer or close the question if your problem is solved, thanks.

avatar image mrlovepump · Nov 14, 2013 at 02:55 PM 0
Share

Have done, wouldn't let me when I originally posted.

avatar image JoeStrout · Aug 22, 2014 at 11:19 PM 2
Share

This works only for a single sprite that's the whole texture file; it doesn't work when you have a sprite sheet with a bunch of sprites defined. In that case you have to use LoadAll (with the path to the sprite sheet) to get all the sprites at once, and then inspect the resulting array to find what you need.

avatar image $$anonymous$$ JoeStrout · May 09, 2016 at 10:06 AM 0
Share

Thanks for this post! I am working with this issue and this really helps me a lot!

avatar image esitoinatteso · May 30, 2015 at 04:00 PM 0
Share

Neat! To assign the loaded sprite you can also use this ( for Image):

     .GetComponent<Image>().overrideSprite = //Your sprite
 

I'm pretty sure that if you use .sprite ins$$anonymous$$d you won't get the job done

avatar image
1

Answer by Melontxo88 · Jul 17, 2017 at 01:21 PM

public Image myImage;

myImage.sprite = (Sprite) Resources.Load("YourFolderName/YourSpriteName",typeof(Sprite));

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ivanrouman · Jan 09, 2018 at 07:21 PM 0
Share

For me works next two lines of code:

SpriteRenderer renderer = gameObject.GetComponent();

renderer.sprite = Resources.Load("YourFolderName/YourSpriteName");

Need to write full path to Resources folder. All files need which you need to use must be stored in resources folder:)

Your comment help me a lot

thanks

avatar image
0

Answer by U_Ku_Shu · Aug 03, 2016 at 12:57 PM

Resources.Load are searching in the directory "Assets/Resources" That's why you need to do

 _sprites = Resources.LoadAll<Sprite>(spritesPath);

or

 _sprites = Resources.Load<Sprite>(spritesPath);

with spritesPath as relative path. If you need to load all from folder "Assets/Resources/Sprites", you need to write only "Sprites".

after this you can just do the following:

 var sprite = sprites[0];

or

 var sprite = _sprites.Where(a => a.name == "Sprite_Name_Needed").First();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite just disappear on scene mode 0 Answers

[SOLVED] Sprite showing up in "Scene", but not in "Game" 11 Answers

Moving pivot point For a 2d sprite 3 Answers

Unity 4.3 Any chance Sprite mesh will become accessible in futur update ? 1 Answer

How to efficiently populate scene with GameObjects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges