Resources.Load

Hey all,

I’ve been working on a kind of collectible card game. I’m making a menu screen where the Sprites representing the cards switch the click of a button.
I seem to be stuck on a particular bit of code. Specifically I’m trying to get the script to Load the initial Sprite I want on-screen.
The cards are numbered 1, 2, 3 and so on. Card 1 will always be the first card on-screen.

public int slotNum;
	public string cardID;
	public Sprite cardIMG;
	private GameObject ImgHolder;

So cardID will be 1 when the scene loads; this happens because i have a set Int that I convert to a String.

IMGHolder is the actual object which holds the Sprite Renderer, it’s a game object separate to the one the code is on so I use a Find to get it;

ImgHolder = GameObject.Find ("CardIMG" + slotNum);
		cardID = slotNum.ToString();
		cardIMG = ImgHolder.GetComponent<SpriteRenderer> ().sprite;
		cardIMG = Resources.Load <Sprite>(cardID);

So the sequence of events is it finds the CardIMG1 (IMGHolder), converts the slotNum to a String which I feed into the GetComponent and Resources.Load to get the Sprite Renderer and the Sprite I want.
However the sprite is never displayed. Everything seems to work right up until Load. I’ve checked and I have a Resources folder (with the Sprite labelled as “1”) spelled correctly.

I’ve been able to figure out a lot of this myself but I’ve been working (on this project) since about 9am this morning (nearly midnight here) so my brain’s a bit fried. If anyone can offer a helpful pointer or two I’d appreciate it.

The reason is you are assigning the sprite to a new variable.

cardIMG = ImgHolder.GetComponent<SpriteRenderer> ().sprite;//Here you are assigning a new sprite to this CardIMG;
cardIMG = Resources.Load <Sprite>(cardID);//And here you just change cardIMG sprite value to a new one;


ImgHolder.GetComponent<SpriteRenderer> ().sprite = Resources.Load <Sprite>(cardID);//change to this.