I'm trying to create an object that will be responsible of creating and showing different sprites, so I would like to access directly the assets/sprites programmatically instead of drag and drop a sprite in the hierarchy under that object.
There's a way to programmatically create a new sprite and assign what I have in the assets folder?
I also would like to have a sort of data structure with a few images loaded at the start of the game, an array or a dictionary or something like that so I can change based on some situation which images I need to show. But what confuses me since I'm new to Unity is how to create a sprite taking the reference to the sprite programmatically using the assets folder.
edit with progress:
I've created an array of sprites like this:
public Sprite[] mySprites;
in Unity I've added sprites inside the array manually (I just dragged png's inside the variable array) so actually in this Object I have a Component with this array full of sprites
inside the component I also have done this:
public SpriteRenderer renderer;
renderer = transform.GetComponent<SpriteRenderer>();
renderer.sprite = (Sprite)mySprites [0];
I got no error until I run the game, then the sprite is NOT assigned and I got this:
"PPtr cast failed when dereferencing! Castin from Texture2D to Sprite!"
I got this error even without casting (Sprite) and btw I don't know why he is telling my about Texture2D since verything is setted as sprite