Why do my image sprites only change after closing the run, instead of when I start the game?
What I mean by this is that, upon starting the run, it does nothing. But once I hit the play button to end the run, it suddenly changes the images of shirtSpace
and pantsSpace
in both the game window and editor?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClothesScript : MonoBehaviour
{
// Variables
Image shirtRenderer;
Image pantsRenderer;
Sprite[] shirtArray;
Sprite[] pantsArray;
public GameObject shirtSpace;
public GameObject pantsSpace;
int currentShirtIndex = 0;
int currentPantsIndex = 0;
// Start is called before the first frame update
void Start()
{
shirtRenderer = shirtSpace.GetComponent<Image>();
pantsRenderer = pantsSpace.GetComponent<Image>();
shirtArray = new Sprite[4];
pantsArray = new Sprite[4];
LoadArrays();
UpdateShirt();
UpdatePants();
}
// Update is called once per frame
void Update()
{
}
// LoadArrays is called by Start to fill the arrays
void LoadArrays()
{
shirtArray[0] = Resources.Load<Sprite>("Shirt0");
shirtArray[1] = Resources.Load<Sprite>("Shirt1");
shirtArray[2] = Resources.Load<Sprite>("Shirt2");
shirtArray[3] = Resources.Load<Sprite>("Shirt3");
pantsArray[0] = Resources.Load<Sprite>("Pants0");
pantsArray[1] = Resources.Load<Sprite>("Pants1");
pantsArray[2] = Resources.Load<Sprite>("Pants2");
pantsArray[3] = Resources.Load<Sprite>("Pants3");
}
// UpdateShirt is called by Start and whenever the currentShirtIndex is changed
void UpdateShirt()
{
shirtRenderer.sprite = shirtArray[2];
Debug.Log("Cleared Shirt");
}
// UpdatePants is called by Start and whenever the currentPantsIndex is changed
void UpdatePants()
{
pantsRenderer.sprite = pantsArray[currentPantsIndex];
Debug.Log("Cleared Pants");
}
// ShirtDecrease is called when ShirtLeftButton is clicked
public void ShirtDecrease()
{
if(currentShirtIndex <= 1)
{
currentShirtIndex = 3;
}
else
{
currentShirtIndex--;
}
}
// ShirtIncrease is called when ShirtRightButton is clicked
public void ShirtIncrease()
{
if(currentShirtIndex == 3)
{
currentShirtIndex = 1;
UpdateShirt();
}
else
{
currentShirtIndex++;
}
UpdateShirt();
}
// PantsDecrease is called when PantsLeftButton is clicked
public void PantsDecrease()
{
if(currentPantsIndex <= 1)
{
currentPantsIndex = 3;
}
else
{
currentPantsIndex--;
}
}
// PantsChange is called when PantsRightButton is clicked
public void PantsIncrease()
{
if(currentPantsIndex == 3)
{
currentPantsIndex = 1;
}
else
{
currentPantsIndex++;
}
}
}
shirtRenderer.sprite = shirtArray[2];
inUpdateShirt()
seems wrong \$\endgroup\$*Decrease()
and*Increase
never loads the image at location 0.*Pants()
never calls theUpdatePants()
\$\endgroup\$