Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by losingisfun · Sep 23, 2016 at 11:44 AM · arraysstatic variablespritemanager

C# - How to set the sprites in a Static Sprite[] array

I have an image GameObject that changes Sprite depending on the inventory page. So when a user clicks an item, it changes the image to match that. I'm trying to store all of the possible sprites into a Sprite[]. it makes it much easier for coding reasons. However, I'm having a lot of trouble.

I originally started with a public Sprite[], and tried to set it up with public Sprite variables. But for some reason it was always returning null unless i set it up as static. My Sprite[] is now static, as well as the Sprite variables i want to put into the array. However, i can't click and drag the sprites into their public variables anymore, through the inspector, because they are now static, and I don't know how to set sprites any other way.

Now I'm somewhat stuck. I even tried (shamefully) to make a bunch of public Sprites, and then tried to set the static Sprites by the non-static ones, - like public static image0 = otherImage; but apparently non-static sprites won't be accepted into a static sprite variable.

I've tried the Resources.LoadAll method, but it's confusing, and i can't figure out how to SEE let alone set the images up in the right order into my Sprite[].

Any help would be appreciated.

EDIT:

Here is how i've been trying to display the Sprite:

 int imageNumber = weaponList [page, 20];
         spriteToUse = imageList [imageNumber];

and here is how i've been trying to set it (as non-static):

    public Sprite image0;
    public Sprite image1;
    public Sprite image2;

         public Sprite[] imageList = new Sprite[] {
                       image0,
                       image1,
                       image2,
                      ...
     };

If I create a static Sprite[] and static Sprites to fill it, it will recognise it, and not get a null exception when i try to get it. However, I don't know how to properly set static Sprites into a Sprite array. (I normally just use the inspector to drop and drag sprites onto their public variable placeholders.)

Comment
Add comment · Show 6
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 NoseKills · Sep 23, 2016 at 03:28 PM 0
Share

$$anonymous$$any of the things you have tried sound like they should be doable. If we saw some code we could perhaps give tips on how to fix the problems you mention.

Where is the array and how are you using the sprites from it?

avatar image losingisfun NoseKills · Sep 24, 2016 at 06:46 AM 0
Share

ok, ill add some code

avatar image tanoshimi · Sep 24, 2016 at 07:01 AM 0
Share

static is not (and, in 99.9% of cases, never is) the answer.

 public Sprite[] imageList;

with sprites assigned in the inspector is just fine. Your problem is with the code that generates the null reference error - what is that?

avatar image losingisfun tanoshimi · Sep 24, 2016 at 07:53 AM 0
Share

Well thats what I'm confused about. When its static, it won't get the error, but when its plain old public it does.

avatar image losingisfun tanoshimi · Sep 24, 2016 at 07:59 AM 0
Share

I'm getting index out of range error, which doesn't make sense, because even if i change it to 0 or 1, and i know for sure that I've set it to more than that, it still says its out of range...

avatar image losingisfun tanoshimi · Sep 24, 2016 at 08:05 AM 0
Share

hang on, I'm also getting told that my field initialiser cannot reference a non-static method or property. (That may explain the out of range error). so my Sprite[] = new Sprite[] {image0, ...}; all get errors, because none of them are static. EDIT: nope, i fixed this by moving it into void Start() {} and I'm still getting out of range error.

1 Reply

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

Answer by losingisfun · Sep 24, 2016 at 08:40 AM

I've figured it out.

I hope this helps someone else in the future.

To make a Sprite[] array, you need to do the following:

 public Sprite[] spriteList;

Then through the inspector, set the size of the array, and drag and drop your sprites onto the empty elements within the inspector.

You do not need to create other variables to hold sprites, they will all be held in the Sprite array.

trying to set or change a Sprite array during runtime doesn't seem to work, everything gets ignored (I'm assuming this is a unity thing) you have to do it in the inspector. You cannot set the size of the array in your script, it has to be done in the inspector. (that too, gets ignored)

(Seriously, I'm shocked this hasn't been explained in the API - Documentation, it is not intuitive to know this)

Comment
Add comment · Show 4 · 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 tanoshimi · Sep 24, 2016 at 10:36 AM 3
Share

Setting or changing an array at runtime works just fine. What you're getting thrown by, I suspect, is how variables are serialised and initialised, which is somewhat esoteric.

A private variable (array or otherwise), by default, is not serialised. That means you can declare it and use it in code as normal, but it won't appear in the inspector. A public variable is serialised. Crucially, any values assigned to a public variable in the inspector will override the initalisation declared in code. So if you have a variable declared in a component as follows:

 public int number = 2;

but in the inspector for that component assign a value of 5, say: alt text

Then number will have a value of 5, not 2. From your description, it sounds like you declared a public sprite array in code, but did not assign any sprites in the inspector. Thus, your sprite array was being initialised as an empty array, which is why you got null reference errors.

Note that, unless, the values in that array need to be accessed by a method in another class, you shouldn't be declaring it as public anyway. If you simply set spriteList as private then you can initialise it in code as you were expecting. Alternatively, set it as public but use the inspector for initialisation.

You might find this useful reading: https://docs.unity3d.com/$$anonymous$$anual/script-Serialization.html

avatar image losingisfun tanoshimi · Sep 24, 2016 at 01:30 PM 0
Share

yes, however i never even touched the inspector. So even after attempting to set size, and all the elements by code, the inspector (which i never engaged with) was overriding it. However, if i were to normally leave a public int variable, and not touch it via the inspector, it would still let the script deter$$anonymous$$e its value, and the inspector would normally inherit those values, but the Sprite array, decides to behave slightly differently.

avatar image Bonfire-Boy losingisfun · Sep 24, 2016 at 02:02 PM 1
Share

The inspector value overrides any initialisation value set when you declare the variable in code, but that's all. You can still set it in code (eg in the Awake() or Start() function).

If all your sprites are going to share the sprite array, you could put it into some kind of manager class, so they don't need to duplicate the data.

avatar image Hoppalot22 · Jul 21, 2018 at 11:34 AM 0
Share

THAN$$anonymous$$ YOU.

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

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

Related Questions

Dynamic multi-dimensional array in C#? 2 Answers

accessing gameObject script from an array 3 Answers

ios cant access variables in array 1 Answer

Multidirectional arrays on javascript 2 Answers

Problem with countdown timer with texture using an array 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