Change Sprite according to user choice
Hi all,
I'm making a small car race game where the player has to key tap his car to the finish line in order to win.
I want the user to be able to choose between 15 cars to use in the race.
I have a scene (called TrophyRun) where he can cycle through the cars and then hit play (taking you to the race scene).
Can someone help me understand how I can change the car sprite in the game scene so it matches the one that the player previously selected?
This is the script to cycle through the cars:
public class CarSelection : MonoBehaviour
{
public Sprite[] gallery; //store all your images in here at design time
public Image displayImage; //The current image thats visible
public Button nextCar; //Button to view next image
public Button previousCar; //Button to view previous image
private int i = 0; //Will control where in the array you are
void OnEnable()
{
//Register Button Events
nextCar.onClick.AddListener(() => NextCarButton());
previousCar.onClick.AddListener(() => PreviousCarButton());
}
public void NextCarButton()
{
if (i + 1 < gallery.Length)
{
i++;
}
}
public void PreviousCarButton()
{
if (i - 1 >= 0)
{
i--;
}
}
void Update()
{
displayImage.sprite = gallery[i];
}
}
I have tried the below script but it doesn't seem to work:
playerCar.GetComponent<SpriteRenderer>().sprite = GameObject.Find("CarSelection").GetComponent<CarSelection>().displayImage.sprite;
If anyone can give me some suggestions, that would be great!
Thanks in advance.
Your answer
Follow this Question
Related Questions
Glowing 2D items 0 Answers
Button Sprite Stretching 0 Answers
How to resize an image, without affecting the size of the button? 0 Answers
Programmatically creating buttons with images that can be one of dozens. 0 Answers
Download images and apply to sprite 0 Answers