Home > Back-end >  How do I convert my string into a unity sprite?
How do I convert my string into a unity sprite?

Time:11-12

I am making a memory game and when I press a card it's supposed to "flip". (Simple deck of cards ace-9) I've added the code below to a function "addAcard" but I have to convert my string into a sprite to be able to call it in the assets where I have stored pictures of all of my cards. The rank is the inparameter in the function, 0-9 as the cards expected values. Summary of the problem is that the sprite s1 stays empty.

        c.name = "" rank;
        string nameOfCard = "";
        string cardNumber = "";
        if(rank == 0)
            cardNumber = "ace";
        else
            cardNumber = ""   (rank 1);
        nameOfCard = cardNumber   "_Of_Hearts";
        Sprite s1 = (Sprite)(Resources.Load<Sprite>(nameOfCard));
        print ("s1:"   s1);
        GameObject.Find("" rank).GetComponent<tile> ().setOriginalSprite (s1);

I've tried looking around as it may been a typo I can't find or the row where I create s1 itself doesn't work anymore since the guides I'm watching could be done on different versions of Unity. I'm on 2021.3.8f1. Both the nameOfCard and cardNumber creates the expected results.

CodePudding user response:

Not necessarily the answer but you might want to print the "nameOfCard" variable too. Is the variable-name as expected? i.e. "ace_Of_Hearts" Maybe the image isn't inside the Resources folder or if it is, then maybe it's inside another folder, adding an extra directory to add to the Loading-Path like "cards/ace_Of_Hearts" Have you set your image as a Sprite inside the Editor-Inspector? Unity labels images as textures by default. These are a few things that come to mind for me, since the code itself does not look broken, though I do have recomendations:

Maybe as an alternative, create a ScriptableObject-Script. There you can assign all the cards directly and during runtime only have to load the single scriptable objects to get all the images at once. Then you can put the Resource.Load-Function into the Start-Function and don't have to deal with loading Assets during runtime. If you don't want that, you can at least cache the images in an Dictionary<string,Sprite> so you don't have to load the same image twice.

CodePudding user response:

Check the sprite is named exactly the value of the string passed to Resources.Load<Sprite>?

Go into debug and put a stop on this line nameOfCard = cardNumber "_Of_Hearts"; and check it exactly matches the name of the sprite.

Also, is the sprite in a folder called "Resources" in your assets folder? You can have subfolders, but they all have to be in the Resources folder for this command to work.

enter image description here

I use the following code to load sprites from a subfolder called "Icons" within the unity Asses/Resources folder.

if (maptile.HexType == 30) { s1 = "ZI11"; }  
myZomspriteRenderer.sprite = Resources.Load<Sprite>("Icons/"   s1);
myZomspriteRenderer.enabled = true;
  • Related