On my scene view in unity, I have three gameobjects set up, each with a respective sprite renderer and sprites. Two of the three are smaller in dimensions than the third one.
The images displayed by both smaller sprites are different. What I'd like to do is to be able to change the sprite of the third one by clicking on the other two. Clicking on one of the smaller sprites will make the third larger sprite match the small sprite that was previously clicked.
Here are the relevant scripts assigned to the two smaller sprites.
1st one that handles which sprite is displayed on the small sprite:
public class CardModel : MonoBehaviour
{
SpriteRenderer spriteRenderer; // renders sprite
public int cardIndex; // indicates what the card is
public bool show; // shows or hides card
public Sprite[] cardFronts; // collection of cards' front sides
public Sprite cardBack; // backside of all cards
// if card is turned up, show front of card, show back otherwise
public void DisplaySprite()
{
if (show)
spriteRenderer.sprite = cardFronts[cardIndex];
else
spriteRenderer.sprite = cardBack;
}
void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>(); // retrieve our sprite renderer
DisplaySprite(); // show card's sprite
}
}
And 2nd one that makes the smaller sprites clickable:
public class ClickCard : MonoBehaviour
{
CardModel model; // used to retrieve card index and bool to assign to larger sprite
public GameObject largeSprite; // third large sprite
void OnMouseDown()
{
CardModel cModel = GetComponent<CardModel>(); // retrieve card model from smaller sprite
LargeCardModel largeCardModel = largeSprite.GetComponent<LargeCardModel>(); // retrieve card model for larger sprite
largeCardModel.matchSprite(cModel.show, cModel.cardIndex); // match large sprite with small sprite clicked
}
}
And finally, the script attached to the third large sprite:
public class LargeCardModel : MonoBehaviour
{
public int largeIndex; // determines which sprite to show
public Sprite[] largeCardFaces; // collection of large sprites
public Sprite largeCardBack; // large card back
SpriteRenderer largeRenderer; // renders the image upon the large sprite
void Awake()
{
largeRenderer = GetComponent<SpriteRenderer>(); // retrieve renderer
}
// method to allow sprite to be changed
public void matchSprite(bool showFace, int cIndex)
{
if (largeRenderer == null)
largeRenderer = GetComponent<SpriteRenderer>(); // for some reason, I need to include this line or else I get an exception
if (showFace)
largeRenderer.sprite = largeCardFaces[cIndex];
else
largeRenderer.sprite = largeCardBack;
}
}
For whatever reason, I can't get the image to the larger sprite to change during runtime however when I stop running the program, the large sprite changes to that of the last smaller sprite I clicked on.