Sorry for the weird topic title, but I didn’t know how else to explain it.
I have an image in my scene called “skill1”. It has a box collider on it with “is trigger” enabled.
On this image i have this script:
public static int freeSkillPoints;
public static int requiredSkillPoints;
public static int _skillLevel = 0;
void Start()
{
freeSkillPoints = 20;
if ((gameObject.name == "skill1") && (freeSkillPoints >= requiredSkillPoints))
{
// Set new alpha to make the image appear available.
GetComponent<SpriteRenderer>().color = new Color(.8f, .8f, .8f);
}
}
private void OnMouseDown()
{
//Check if the player is clicking the image and that it has not been clicked before.
if ((gameObject.name == "skill1") && (_skillLevel == 0))
{
// Set the required skillpoints for the first level.
requiredSkillPoints = 1;
if ((freeSkillPoints >= requiredSkillPoints))
{
//Add the first skill level.
_skillLevel ++;
//deduct skillpoints
freeSkillPoints -= 1;
Debug.Log("You've upgraded the Skill to level "+ _skillLevel +". You Have " + freeSkillPoints + " Skillpoints Left.");
// Full alpha to show that the skill has been selected.
GetComponent<SpriteRenderer>().color = new Color(1, 1, 1);
}
//If the image has already been clicked once (upgrading from level 1 to 2)
if ((_skillLevel == 1) && (freeSkillPoints >= requiredSkillPoints))
{
_skillLevel ++;
freeSkillPoints -= 5;
Debug.Log("You've upgraded the Skill to level " + _skillLevel + ". You Have " + freeSkillPoints + " Skillpoints Left.");
}
//If the image has already been clicked twice (upgrading from level 2 to 3)
if ((_skillLevel == 2) && (freeSkillPoints >= requiredSkillPoints))
{
_skillLevel ++;
freeSkillPoints -= 10;
Debug.Log("You've upgraded the Skill to level " + _skillLevel + ". You Have " + freeSkillPoints + " Skillpoints Left.");
}
//If the player hasn't got the required skillpoints for upgrading.
else if (freeSkillPoints < requiredSkillPoints)
{
Debug.Log("You Do not have the required skillpoints to upgrade the Skill. You need " + (requiredSkillPoints - freeSkillPoints) + " more.");
}
}
}
I know this is messy coding. There must be an shorter/cleaner way of coding this.
But my issue is that, the nested if statement just runs all the way through and upgrades the skill 3 times on a single mouseclick.
How do I set up the code, so that it stops after each click/upgrade and runs again when the player clicks the image again?
Is it possible or is there a better way of doing this?
Thank you in advance.