How do I check for mouseclicks inside a nested if statement.
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.
_skill1 += 1; and _S$$anonymous$$ill1 += 1; are not the same. Is that a typo or is it intentional?
Answer by ilmix123 · Mar 07, 2017 at 08:29 PM
Try using else if
instead of if. Or add return;
statements at the end of each if
block.
Thank you for helping me. Using else if
worked. Thank you.
Answer by Properpeanut · Mar 07, 2017 at 07:56 PM
Sorry, that's a typo... Changed some of the words in the script for the question to make it a little less specific. Fixed it.
Now your 2nd and 3rd if
statements are the same. If you fix that I think your code should work as intended.
The problem is that if the player has enough skillpoints to upgrade multiple times, it just upgrades it all the way. The player can't choose only to upgrade to level1.
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)
// STOP HERE AND LET THE PLAYER DECIDE IF HE/SHE WANTS TO UPGRADE AGAIN BY CLIC$$anonymous$$ING AGAIN
if ((_skillLevel == 2) && (freeSkillPoints >= requiredSkillPoints))
{
_skillLevel ++;
freeSkillPoints -= 10;
Debug.Log("You've upgraded the Skill to level " + _skillLevel + ". You Have " + freeSkillPoints + " Skillpoints Left.");
}
To stop execution there do either:
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)
// STOP HERE AND LET THE PLAYER DECIDE IF HE/SHE WANTS TO UPGRADE AGAIN BY CLIC$$anonymous$$ING AGAIN
else if ((_skillLevel == 2) && (freeSkillPoints >= requiredSkillPoints))
{
_skillLevel ++;
freeSkillPoints -= 10;
Debug.Log("You've upgraded the Skill to level " + _skillLevel + ". You Have " + freeSkillPoints + " Skillpoints Left.");
}
or
if ((_skillLevel == 1) && (freeSkillPoints >= requiredSkillPoints))
{
_skillLevel ++;
freeSkillPoints -= 5;
Debug.Log("You've upgraded the Skill to level " + _skillLevel + ". You Have " + freeSkillPoints + " Skillpoints Left.");
return;
}
//If the image has already been clicked twice (upgrading from level 2 to 3)
// STOP HERE AND LET THE PLAYER DECIDE IF HE/SHE WANTS TO UPGRADE AGAIN BY CLIC$$anonymous$$ING AGAIN
if ((_skillLevel == 2) && (freeSkillPoints >= requiredSkillPoints))
{
_skillLevel ++;
freeSkillPoints -= 10;
Debug.Log("You've upgraded the Skill to level " + _skillLevel + ". You Have " + freeSkillPoints + " Skillpoints Left.");
}
or both.
Your answer
Follow this Question
Related Questions
script not returning values in if statements nested in for loop 1 Answer
The bool activates and deactivates but the script in the if statement doesn´t work 1 Answer
Why does my if statement skip the body even when the response is true? 1 Answer
Enumaration error CS1025: Single-line comment or end-of-line expected 1 Answer
code so if 1 enemy isnt killed every 4 seconds player dies? 0 Answers