- Home /
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.)
$$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?
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?
Well thats what I'm confused about. When its static, it won't get the error, but when its plain old public it does.
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...
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.
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)
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:
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
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093334im_/https://answers.unity.com/themes/thub/images/avi.jpg)