Search Unity

in void update what is the command to end something?

Discussion in 'Getting Started' started by Sylvir, Apr 25, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    for example i have this piece of code here.

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (currentExperience >= 500)
    4.         {
    5.             fishingRank += 1;
    6.  
    7.         //    DeterminRequieredXP();
    8.        
    9.         }
    10.     }
    if i just simply leave it here in the void update it will just scroll my rank up infinatly once i hit the 500 experience points.

    (if i remember correctly there is some kind of command, like end, or return or something that would check for it until it happens then stop, but i am not sure what it is and having issues trying to search for it in the doumentation without remembering the keyword. )

    or should i put some kind of check like this in another place other then void update?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Easiest way to do this is simply use a bool flag.

    Code (CSharp):
    1. bool hasLeveledUp = false;
    2.  
    3. void Update ()
    4.     {
    5.         if (!hasLeveledUp && currentExperience >= 500)
    6.         {
    7.             fishingRank += 1;
    8.             hasLeveledUp = true;
    9.  
    10.         //    DeterminRequieredXP();
    11.      
    12.         }
    13.     }
    There are better ways to do this, but this fits best with what you have so far, and will do a decent enough job as you learn.
     
  3. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    cool, thank you, would the better ways to do that involve a foreach loop, or coroutine? i haven't used those too much yet but originally i thought one of those maybe a better way to do it.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I would set it up in the setter of experience.

    Code (CSharp):
    1. int level;
    2. int levelThreshold;
    3. int _experiance;
    4. int experiance {
    5.     get {
    6.         return _experiance;
    7.     }
    8.     set {
    9.         _experiance = value;
    10.         if (_experiance > levelThreshold) {
    11.             // level up animation
    12.             level++;
    13.             levelThreshold += 500;
    14.         }
    15.     }
    16. }
     
    Sylvir likes this.
  5. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    oh okay, so in order to get the values between scripts i need to put a {get; set;} part into the code for each one i want to get and set the value for in that script?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The get and set methods are called properties. From the outside they are almost identical to fields. But you can do all sorts o interesting things with the data when you receive it.

    Some common uses of properties
    • Data validation
    • Read or write only data
    • Triggering an event when data changes
    • Modifying other data when data changes
    • Exposing the same data in multiple ways. (Eg exposing the same value of time in terms of hours, minutes, seconds and days)
    A couple of things to note about properties that make them different to fields.
    • Unity doesn't serialise them, meaning they don't show up in the inspector
    • They behave differently to fields under reflection.
     
    Sylvir and Ryiah like this.