Search Unity

Lock a variable value?

Discussion in 'Scripting' started by mojokage, May 30, 2015.

  1. mojokage

    mojokage

    Joined:
    Feb 15, 2014
    Posts:
    8
    So I'm working on a leaderboard that stores up to 3 scores at at time. I've made it so that:
    Code (JavaScript):
    1. var scoreNo1 : int =0;
    2. var scoreNo2 : int =0;
    3. var scoreNo3 : int =0;
    4.  
    5. startTime = Time.timeSinceLevelLoad;
    6.  
    7. newScore = startTime;
    8.  
    9. if (deathMover.newScore > scoreNo1){
    10. scoreNo3 = scoreNo2;
    11. scoreNo2 = scoreNo1;
    12. scoreNo1 = deathMover.newScore;
    13. }
    14.  
    15. else if (deathMover.newScore > scoreNo2){
    16. scoreNo3 = scoreNo2;
    17. scoreNo2 = deathMover.newScore;
    18. }
    19.  
    20. else if (deathMover.newScore > scoreNo3){
    21. scoreNo3 = deathMover.newScore;
    22. }
    23. }
    The function that contains the If Statements is called within the Start Function and the "startTime" & "newScore" statements are within other scripts.

    The problem is that scoreNo1 is constantly being overwritten no matter what. If I could lock it down and make it so that once the newScore was passed to scoreNo1, scoreNo1 keeps that value until it's beaten (and then passes it down to scoreNo2) that would be great!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    where is this code being executed? what is deathMover? this script, if so why are you using "deathMover.newScore" and "newScore"?

    you "lock down" a variable by not updating it when you don't want to... so we really need to know more about what is going on if you want help spotting where your logic flow is going wrong.