Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Properpeanut · Mar 07, 2017 at 06:53 PM · if-statementsnested

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ilmix123 · Mar 07, 2017 at 07:44 PM 0
Share

_skill1 += 1; and _S$$anonymous$$ill1 += 1; are not the same. Is that a typo or is it intentional?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Properpeanut · Mar 07, 2017 at 08:45 PM 1
Share

Thank you for helping me. Using else ifworked. Thank you.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ilmix123 · Mar 07, 2017 at 08:04 PM 0
Share

Now your 2nd and 3rd if statements are the same. If you fix that I think your code should work as intended.

avatar image Properpeanut · Mar 07, 2017 at 08:26 PM 0
Share

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.");
              }

   

avatar image ilmix123 · Mar 07, 2017 at 08:46 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

94 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges