• 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
0
Question by Sandbird_ · Oct 20, 2015 at 07:01 PM · spritesarrays

Assign different sprites to values in an array

I am trying to assign different sprites into an array called sprites. I want each value of the array to hold a different spritesheet. Right now i am getting this error i dont understand why... :

 Cannot implicitly convert type `UnityEngine.Sprite[]' to `UnityEngine.Sprite'

In my public declaration i have:

 public static int MAIN_SPRITE = 1;
 public Sprite[] sprites;

further down in my void Start () i have:

 void Start () {
     sprites = new Sprite[32];
 }

and then i am trying to assign the spritesheet in my init() method like this:

 sprites[MAIN_SPRITE] = Resources.LoadAll<Sprite>("mainImg");

where mainImg is the name of my sprite sheet.

What am i doing wrong ? (The error appears for my last command where i set the array)

-Thanks

Comment
Add comment
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

3 Replies

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

Answer by BeauWorlds · Oct 22, 2015 at 08:14 AM

Hi Sandbird,

You are getting this because sprites[MAIN_SPRITRE] refers to a single sprite and not all the array, use this instead: sprites = Resources.LoadAll("mainImg") as Sprite[];

I've never used Resources.LoadAll but I think it returns an array, so you can not set a single element to it's returned array. I believe also that it returns an array of objects, so you will need to cast this object array to a sprite array using "as Sprite[]"

I hope that helps :)

If you have any questions, leave a comment and let me know if it works :)

Beau C

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
avatar image
1

Answer by hexagonius · Oct 20, 2015 at 09:34 PM

 Resources.LoadAll<Sprite>("mainImg");

is already returning an array. Just assign it to the array directly without the indexer

Comment
Add comment · Show 3 · 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 Sandbird_ · Oct 20, 2015 at 09:39 PM 0
Share

i wont to populate the sprites array with many tiles which i'll use in code, For example:

 sprites[$$anonymous$$AIN_SPRITE] = Resources.LoadAll<Sprite>("mainImg");
 sprites[$$anonymous$$AIN_EXP] = Resources.LoadAll<Sprite>("explosions");
 ....

where $$anonymous$$AIN_SPRITE and $$anonymous$$AIN_EXP are gonna be integers co$$anonymous$$g from other scripts.

avatar image hexagonius Sandbird_ · Oct 21, 2015 at 08:10 PM 0
Share

Then you'll need an array of Sprite arrays.

 public static int $$anonymous$$AIN_SPRITE = 1;
 public Sprite[][] sprites;
 
 void Start () {
    sprites = new Sprite[32][];
 }
 
 sprites[$$anonymous$$AIN_SPRITE] = Resources.LoadAll<Sprite>("mainImg");

Accessing one of them would be:

 sprites[$$anonymous$$AIN_SPRITE][0]

for example

avatar image Sandbird_ hexagonius · Oct 22, 2015 at 08:00 AM 0
Share

I am co$$anonymous$$g from a PHP background and jagged array to mimic a normal array hurts my eyes.lol. Hmmm, so i guess even if the sprites object was a GameObject i wouldnt be able to save each tile in one value uh ? i have to trick it with jagged arrays ? I cant have an array with whatever i want inside for values ? Strings or Arrays ? example:

 sprites[0] = "a string";
 sprites[1] = "{array}";
 ....etc

thanks

avatar image
0

Answer by leejponce · Aug 04, 2020 at 04:34 PM

You don’t have to trick anything. You can assign your sprites with values and call that up at specific times. If you want to start with a specific sprite you put it in the [0] and call that in the void Start. And then you can change it in a method afterwards.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

How to make a rolling meter like in Earthbound. 1 Answer

Get Sprite index in array by filename 1 Answer

2D Sprite Trouble 2 Answers

Using the slider to cycle through an array of sprites? 0 Answers

How to use arrays 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