Material of last four, is set to the first four in the selection.
Alright so I've created an In-game shop, however when I select 1-12 It sets the property just fine. But when I select 13-16 Instead of changing to the color shown, it changes to 1-4 I.E(13=1, 14=2, 15=3, etc.) Instead of displaying the correct colors. They are 4 across, and 4 down in the selection, equaling 16 total selections. Here is the code:
The over all sprite is 1024x1024, then it's split into 16 sections(256x256) 4 rows across, and 4 rows down. So say 1 = Blue 2 = Green 3 = Red 4 = Grey, 13 = Yellow, 14 = Black, 15 = Orange, 16 = Pink When I select Yellow(13), it changes the material of my object to Blue(1) The last four are the only ones I have an issue with, 1-12 work fine. But this last row is giving me trouble.
private void Start()
{
int textureIndex = 0;
Sprite[] textures = Resources.LoadAll<Sprite> ("Shop");
foreach (Sprite texture in textures) {
GameObject container = Instantiate (shopButtonPrefab) as GameObject;
container.GetComponent<Image> ().sprite = texture;
container.transform.SetParent (shopButtonContainer.transform, false);
int index = textureIndex;
container.GetComponent<Button> ().onClick.AddListener (() => ChangeStackMat(index));
textureIndex++;
}
}
}
public void ChangeStackMat(int index)
{
float x = (index % 4) * 0.25f;
float y = ((int)index / 4) * 0.25f;
if (y == 0.0f)
y = 0.75f;
else if (y == 0.25f)
y = 0.5f;
else if (y == 0.50f)
y = 0.25f;
else if (y == 75f)
y = 0f;
stackMaterial.SetTextureOffset ("_MainTex", new Vector2 (x, y));
}
Answer by mj321 · Aug 31, 2016 at 03:02 PM
else if (y == 75f)
y = 0f;
The 75 should be 0.75.
But you can also replace the whole if-chain by using:
float y = 0.75f - ((int)index / 4) * 0.25f;