- Home /
Button doesn't react properly
Hello, I'm creating a 2D Endless Scroller and I'm experiencing trouble when making the Player choose between all the characters he had purchased from the ingame shop. Basically whenever I click on the "Change Dog" Button, sometimes it right away switches and sometimes it first needs 10 clicks or more to respond. This is how the function works to change the dog, explanation coming below: (Btw, the code is still a bit dumb, trying to create a more cleaner version afterwards i find the solution of my problem)
public void changeChar()
{
List<int> keys = new List<int>();
for(int i=0;i<dogsBought.Count;i++)
{
keys.Add(i);
}
for(int i=0;i<dogsBought.Count;i++)
{
if(currentChara == i&¤tChara!=dogsBought.Count-1)
{
dogs.sprite = dogsBought[keys[i+1]];
break;
}
if(currentChara == i && currentChara == dogsBought.Count-1)
{
dogs.sprite = dogsBought[keys[0]];
break;
}
}
if (dogs.sprite == dog1)
{
moves.SetBool("retriever", true);
moves.SetBool("corgi", false);
moves.SetBool("shibe", false);
moves.SetBool("husky", false);
}
if(dogs.sprite == dog2)
{
moves.SetBool("retriever", false);
moves.SetBool("corgi", false);
moves.SetBool("shibe", true);
moves.SetBool("husky", false);
}
if (dogs.sprite == dog3)
{
moves.SetBool("retriever", false);
moves.SetBool("corgi", true);
moves.SetBool("shibe", false);
moves.SetBool("husky", false);
}
if (dogs.sprite == dog4)
{
moves.SetBool("retriever", false);
moves.SetBool("corgi", false);
moves.SetBool("shibe", false);
moves.SetBool("husky", true);
}
}
dogsBought: This is a Sprite List which is created and filled in Start(). Whenever a player purchases another character in the shop code, I set a playerpref to receive in the start method and to fill the sprite in the list
currentChara: By using currentChara = dogsBought.IndexOf(currentChar); in the Update() Method, I make sure this int always is filled with the index of the current Character selected
currentChar: Same as above but only with the current Sprite
moves: Animator
dog1,2,3,4 = Variables which reference to the sprite which are attached to this script
keys: a List for the index variables
However, I do not think that the entire code is incorrect since the button is working but it is reacting very poorly. For every 5 clicks or something the character switches. But at least in right order.
Anyone can find the mistake?
BR