Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by OctoMan · Jun 19, 2015 at 03:17 PM · buttonclickupgradeonce

Upgrade once per click on Button c#

Hi all,

my problem is maybe just small. I want to go up an age once the player reached an amount of exp. I have one button to click, and connected the function to it. But if i click the button, it goes through all, und it fits.

How can i stop it, once it reached the first statement even if i could upgrade more than 1 times?

 public void GoAgeUp()
     {
                 if (GM.exp >= 500) {
                         Debug.Log ("egypts age");
                         GM.age = 2;
                         EDP.StoneAgeUnitsPanel.SetActive (false);
                         EDP.StoneAgeTurretsPanel.SetActive (false);
                 }
                 if (GM.exp >= 8000) {
                         Debug.Log ("medieval age");
                         GM.age = 3;
                         EDP.EqyptsUnitsPanel.SetActive (false);
                         EDP.EqyptsTurretsPanel.SetActive (false);
 
                 }
                 if (GM.exp >= 12000) {
                         GM.age = 4;
 
                 }
                 if (GM.exp >= 16000) {
                         GM.age = 5;
 
                 }
                 if (GM.exp >= 20000) {
                         GM.age = 6;
 
                 }
         }

Thanks in advance.

Comment
Add comment · Show 2
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 Nerevar · Jun 19, 2015 at 03:23 PM 0
Share
 public void GoAgeUp()
      {
                  if (G$$anonymous$$.exp >= 500) {
                          Debug.Log ("egypts age");
                          G$$anonymous$$.age = 2;
                          EDP.StoneAgeUnitsPanel.SetActive (false);
                          EDP.StoneAgeTurretsPanel.SetActive (false);
                  }
                  else if (G$$anonymous$$.exp >= 8000) {
                          Debug.Log ("medieval age");
                          G$$anonymous$$.age = 3;
                          EDP.EqyptsUnitsPanel.SetActive (false);
                          EDP.EqyptsTurretsPanel.SetActive (false);
  
                  }
                  else if (G$$anonymous$$.exp >= 12000) {
                          G$$anonymous$$.age = 4;
  
                  }
                  else if (G$$anonymous$$.exp >= 16000) {
                          G$$anonymous$$.age = 5;
  
                  }
                  else if (G$$anonymous$$.exp >= 20000) {
                          G$$anonymous$$.age = 6;
  
                  }
          }
  
avatar image OctoMan · Jun 19, 2015 at 03:27 PM 0
Share

That's what i tested before, but it just stops in the first statement and stays there. $$anonymous$$y Button is a new Unity 5 button. Any known issues?

3 Replies

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

Answer by barbe63 · Jun 19, 2015 at 03:36 PM

  public void GoAgeUp()
       {            
                   if (GM.exp >= 20000) {
                           GM.age = 6;
   
                   }
                    else if (GM.exp >= 16000) {
                           GM.age = 5;
   
                   }
                    else if (GM.exp >= 12000) {
                           GM.age = 4;
   
                   }
                    else if (GM.exp >= 8000) {
                           Debug.Log ("medieval age");
                           GM.age = 3;
                           EDP.EqyptsUnitsPanel.SetActive (false);
                           EDP.EqyptsTurretsPanel.SetActive (false);
   
                   }
                   else if (GM.exp >= 500) {
                           Debug.Log ("egypts age");
                           GM.age = 2;
                           EDP.StoneAgeUnitsPanel.SetActive (false);
                           EDP.StoneAgeTurretsPanel.SetActive (false);
                   }
           }

Comment
Add comment · Show 8 · 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 OctoMan · Jun 19, 2015 at 03:41 PM 0
Share

Really? Why? I mean, i will test this!

Ah i think i know why, since the first condition is met, it wont go further. Interesting.

avatar image OctoMan · Jun 19, 2015 at 05:06 PM 0
Share

Sadly, doesn't work as i want, i don't want to skip any statement. I want to goe through all. Any idea?

avatar image barbe63 · Jun 19, 2015 at 06:31 PM 0
Share

Let me understand it good. You need a button that will upgrade your age so why don't you just make G$$anonymous$$.age++. Then you can just make a switch on G$$anonymous$$.age to do your functions. You can then make the button to be interactable only if the experience is enough with a simple script. If this is what you want and you need help to achieve this I can help you on that.

What you want is not clear enough, either you need exp and then the method I provided you is good either you need a button to upgrade and do as I just said.

avatar image OctoMan · Jun 19, 2015 at 06:42 PM 0
Share

I want to upgrade only when the player has >500exp >8000 and so on. But don't want to skip the 500 one when the player already have 9000 exp but not upgraded yet. How would i build this up? Since i can't use case ranges.

avatar image barbe63 · Jun 19, 2015 at 07:13 PM 1
Share

Ok now I think i got it.

If i understood it well what you want is more something like this:

  public void GoAgeUp()
       {
                   if (G$$anonymous$$.exp >= 500 && G$$anonymous$$.age==1) {
                           Debug.Log ("egypts age");
                           G$$anonymous$$.age = 2;
                           EDP.StoneAgeUnitsPanel.SetActive (false);
                           EDP.StoneAgeTurretsPanel.SetActive (false);
                   }
                   else if (G$$anonymous$$.exp >= 8000  && G$$anonymous$$.age==2) {
                           Debug.Log ("medieval age");
                           G$$anonymous$$.age = 3;
                           EDP.EqyptsUnitsPanel.SetActive (false);
                           EDP.EqyptsTurretsPanel.SetActive (false);
   
                   }
                   else if (G$$anonymous$$.exp >= 12000  && G$$anonymous$$.age==3) {
                           G$$anonymous$$.age = 4;
   
                   }
                   else if (G$$anonymous$$.exp >= 16000  && G$$anonymous$$.age==4) {
                           G$$anonymous$$.age = 5;
   
                   }
                   else if (G$$anonymous$$.exp >= 20000  && G$$anonymous$$.age==5) {
                           G$$anonymous$$.age = 6;
   
                   }
           }
Show more comments
avatar image
0

Answer by Flightkick · Jun 19, 2015 at 03:41 PM

When you separate all if statements then every if statement will be checked. To ensure that only one code path will be executed use 'else if'. Furthermore you can invert order in which they are checked. For example if I have 9000 exp I should be in the 'medieval age' but since your first logical condition checks if my exp >= 500 it will trigger that code path. By inverting it you will always get the highest possible rank based on your exp.

         public void GoAgeUp()
         {
             if (GM.exp >= 20000)
             {
                 GM.age = 6;
             }
             else if (GM.exp >= 16000)
             {
                 GM.age = 5;
             }
             else if (GM.exp >= 12000)
             {
                 GM.age = 4;
             }
             else if (GM.exp >= 8000)
             {
                 Debug.Log("medieval age");
                 GM.age = 3;
                 EDP.EqyptsUnitsPanel.SetActive(false);
                 EDP.EqyptsTurretsPanel.SetActive(false);
             }
             else if (GM.exp >= 500)
             {
                 Debug.Log("egypts age");
                 GM.age = 2;
                 EDP.StoneAgeUnitsPanel.SetActive(false);
                 EDP.StoneAgeTurretsPanel.SetActive(false);
             }
         }
Comment
Add comment · Show 2 · 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 OctoMan · Jun 19, 2015 at 03:46 PM 0
Share

Thanks alot, i think it will fix the issue, i will test that soon, thanks for your explaination. Cheers.

avatar image OctoMan · Jun 19, 2015 at 05:03 PM 0
Share

I want to achieve that no age is skipable, if i have 9000 exp it goes instandly to Gm.age = 3. How can i achieve this?

avatar image
0

Answer by jdraper3 · Jun 19, 2015 at 08:53 PM

Create a class to describe your game age:

 public class GameAge
 {
     public int ExpRequired { get; set; }
     public GameObject UnitsPanel { get; set; }
     public GameObject TurrentsPanel { get; set; }
     public string Name { get; set; }
 }

Create a variable in your game manager that keeps track of the current age, and also a list of all of your ages in the game:

         GameManager.Ages = new List<GameAge>();
         GameManager.Ages.Add(new GameAge()
         {
             ExpRequired = 0,
             UnitPanel = EDP.StoneAgeUnitsPanel,
             TurretsPanel = EDP.StoneAgeTurretsPanel,
             Name = "Stone Age"
         });
         GameManager.Ages.Add(new GameAge()
         {
             ExpRequired = 500,
             UnitPanel = EDP.EgyptsUnitsPanel,
             TurretsPanel = EDP.EgyptsTurretsPanel,
             Name = "Egypt Age"
         });
         // Add all of your ages...

         GameManager.CurrentAge = 0;

Add a method to your GameManager that sets the current age by looping through the ages and skiping the ones that have already been activated:

     public void ProgessToCurrentAge()
     {
         for (var x = CurrentAge; x < Ages.Count(); x++)
         {
             if (Ages[x].ExpRequired <= CurrentExperience)
             {
                 Ages[CurrentAge].UnitsPanel.SetActive(false);
                 Ages[CurrentAge].TurretsPanel.SetActive(false);
                 
                 CurrentAge = x;
                 Debug.Log(Ages[CurrentAge].Name + " active.");
             }
         }
     }

I don't know if this will work as-is - I can't test it right now unfortunately, but it should be close at least.

Comment
Add comment · Show 2 · 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 OctoMan · Jun 19, 2015 at 08:55 PM 0
Share

Thanks for your affort but i already have the solution, in the first answer.

avatar image jdraper3 · Jun 19, 2015 at 09:29 PM 0
Share

Not a problem, I didn't see the answer earlier because it was hidden. Glad you got it sorted out! :)

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

23 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

Related Questions

GUI Repeatbutton up? 1 Answer

Unity UI Button not working 1 Answer

Stop clicking through a GUI window 2 Answers

Default Android touch sound 0 Answers

Help? Problem with GUI Text button. 1 Answer


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