in void update what is the command to end something?

for example i have this piece of code here.

void Update ()
    {
        if (currentExperience >= 500)
        {
            fishingRank += 1;

        //    DeterminRequieredXP();
       
        }
    }

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?

Easiest way to do this is simply use a bool flag.

bool hasLeveledUp = false;

void Update ()
    {
        if (!hasLeveledUp && currentExperience >= 500)
        {
            fishingRank += 1;
            hasLeveledUp = true;

        //    DeterminRequieredXP();
      
        }
    }

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.

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.

I would set it up in the setter of experience.

int level;
int levelThreshold;
int _experiance;
int experiance {
    get {
        return _experiance;
    }
    set {
        _experiance = value;
        if (_experiance > levelThreshold) {
            // level up animation
            level++;
            levelThreshold += 500;
        }
    }
}
1 Like

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?

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.
2 Likes