Search Unity

problem with schowing Score correctly on screen

Discussion in 'Scripting' started by karammaks, Sep 21, 2014.

  1. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    Hi
    i have a game which you have to collect some cakes , donuts ....etc , by a hat .
    there are different kinds of those candies which give a particular score when the player catch it by this hat , now i made all those candies give the player 1 score .

    and there is special score which i call it DonutScore i'll take later about it ...

    for Donuts , there are normal Donuts , donutBanana, donutPink and donutChocolate

    and there are special kind of each 1 of those donuts but cover by caramel and nuts , which i call this in the game 'donutBananax , donutPinkx and donutChcx .

    those special candies , if the player cathced them by the hat , the plaer will get 1 score , and will get 1 score to the Special Score >>>DonutScore.

    the DonutScore shown on the left side of the screen as in the picture . i made an idea , that if the player pressed on that SpecialDonut on the left side of the screen , this calculation will happen:

    temp=DonutScore*2
    Score=Score+temp

    here is the DestroyObjectAndAddPoint Class , which it is the trigger class :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyObjectAndAddPoint : MonoBehaviour {
    5.  
    6.    public static int Score=0;
    7.    
    8.     public static int sumDonut=0;
    9.    
    10.  
    11.     //for storing a static reference to this DestroyObjectAndAddPoint script
    12.     private static DestroyObjectAndAddPoint _instance;
    13.     public static DestroyObjectAndAddPoint Instance {
    14.         get {
    15.             return _instance;
    16.         }
    17.     }
    18.  
    19.  
    20.     public void Awake()
    21.     {
    22.         //setup the static reference to this DestroyObjectAndAddPoint script
    23.         _instance = this;
    24.     }
    25. /////////////////////////////////// ///
    26. /// T/////////////////////////////
    27.     int score_donutBanana=0;
    28.     int score_donutChc=0;
    29.     int score_donutPink=0;
    30.     int score_sandwitchCake=0;
    31.     int score_jellyCake=0;
    32.     int score_donutBananax=0;
    33.     int score_donutChcx=0;
    34.     int score_donutPinkx=0;
    35.  
    36.     public  TextMesh scoreText;
    37.     public  TextMesh donutScoreText;
    38.    
    39.  
    40.         public void OnTriggerEnter2D(Collider2D collisionObject){
    41.            
    42.  
    43.                         Destroy (collisionObject.gameObject);
    44.  
    45.                      //////////////////////////////////////////////////////
    46.  
    47.                         donutBanana scoredonutBanana = collisionObject.GetComponent<donutBanana> ();
    48.  
    49.                         donutBananax scoredonutBananax = collisionObject.GetComponent<donutBananax> ();
    50.  
    51.                         donutChc scoredonutChc = collisionObject.GetComponent<donutChc> ();
    52.  
    53.                         donutChcx scoredonutChcx = collisionObject.GetComponent<donutChcx> ();
    54.  
    55.                         donutPink scoredonutPink = collisionObject.GetComponent<donutPink> ();
    56.  
    57.                         donutPinkx scoredonutPinkx = collisionObject.GetComponent<donutPinkx> ();
    58.  
    59.                         sandwitchCake scoresandwitchCake = collisionObject.GetComponent<sandwitchCake> ();
    60.  
    61.                         jellyCake scorejellyCake=collisionObject.GetComponent<jellyCake>();
    62.  
    63.                          
    64.     ///////////////////////////////////////////////////////////////////////////////////////////////////
    65.  
    66.         if (scoredonutBanana != null) {
    67.             score_donutBanana = score_donutBanana + scoredonutBanana.scoredonutBanana1;
    68.            
    69.         }
    70.  
    71.         if (scoredonutBananax != null) {
    72.             score_donutBananax = score_donutBananax + scoredonutBananax.scoredonutBananax1;
    73.            
    74.         }
    75.  
    76.         if (scoredonutChc != null) {
    77.             score_donutChc = score_donutChc + scoredonutChc.scoredonutChc1;
    78.            
    79.         }
    80.  
    81.         if (scoredonutChcx != null) {
    82.             score_donutChcx = score_donutChcx + scoredonutChcx.scoredonutChcx1;
    83.            
    84.         }
    85.  
    86.         if (scoredonutPink != null) {
    87.             score_donutPink = score_donutPink + scoredonutPink.scoredonutPink1;
    88.            
    89.         }
    90.         if (scoredonutPinkx != null) {
    91.             score_donutPinkx = score_donutPinkx + scoredonutPinkx.scoredonutPinkx1;
    92.            
    93.         }
    94.  
    95.  
    96.         if (scoresandwitchCake != null) {
    97.             score_sandwitchCake = score_sandwitchCake + scoresandwitchCake.scoresandwitchCake1;
    98.            
    99.         }
    100.         if (scorejellyCake != null) {
    101.             score_jellyCake = score_jellyCake + scorejellyCake.scorejellyCake1;
    102.            
    103.         }
    104.  
    105.  
    106.  
    107.         Score = score_donutBanana+score_donutChc+score_donutPink+score_sandwitchCake+
    108.             score_jellyCake+score_donutPinkx+score_donutBananax+score_donutChcx;
    109.  
    110.  
    111.             sumDonut=score_donutBananax+score_donutPinkx+score_donutChcx;
    112.                
    113.    
    114.  
    115.                         scoreText.text = "Score : " + Score;
    116.                         donutScoreText.text = ": x " + sumDonut;
    117.                
    118.     }
    119.     public void Accumulate(DonutButton donutButton)
    120.     {
    121.         //reset score accumulator
    122.         sumDonut = 0;
    123.        
    124.         //update text fields
    125.         scoreText.text = "Score : " + Score;
    126.         donutScoreText.text = ": x " + sumDonut;
    127.  
    128.     }
    129.  
    130.  
    131.     }
    132.  
    and this is the code of the DonutButton

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class DonutButton : MonoBehaviour {
    5.     public int scr;
    6.     public void OnMouseDown() {
    7.         //grab the static reference to scoreKeeper
    8.         DestroyObjectAndAddPoint scoreKeeper =DestroyObjectAndAddPoint.Instance;
    9.        
    10.         //perform score calculation
    11.         scr = DestroyObjectAndAddPoint.sumDonut * 2;
    12.         DestroyObjectAndAddPoint.Score = DestroyObjectAndAddPoint.Score + scr;
    13.  
    14.         //make scoreKeeper set sumDonut = 0, and update the text fields
    15.         scoreKeeper.Accumulate(this);
    16.     }
    17. }
    when the player play , and both score was for example the same as the attatched picture
    DonutScore is 2
    and Score is 10

    now if the player pressed on the DonutButton , DonutScore will multiply by 2 >> 4 , and this 4 will be added to the Score , which will be 14 . everything work perfect in the code but the problem is if i collect 1 more candy , Score will be 11 instead of being 15 , so the code doesn't store 14 in Score , it just show it on the screen
    what should i do ?
     

    Attached Files:

    • a2.jpg
      a2.jpg
      File size:
      128.8 KB
      Views:
      664
  2. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    any help please ? plz dont tell me to put the question in answers page cuz no one replies there :confused:
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You have to shorten and optimize your code quite a bit. Try to design a few more methods which allow you to get rid of all the copy/paste stuff. That shouldn't be too hard, since they do not differ a lot.

    Now the actual problem:
    You override your score in the trigger function without taking the bonus score into account.

    Example:

    You've got a score of 10 just like the picture shows and a multiplier for the donuts which is 2.

    Let's say you collect a donut which adds +1 to the total score. You then recalculate the total score in the following line:

    Code (CSharp):
    1. Score = score_donutBanana+score_donutChc+score_donutPink+score_sandwitchCake+
    2.             score_jellyCake+score_donutPinkx+score_donutBananax+score_donutChcx;
    You get the expected result, which will be 11 in this case, since you updated one of these specific scores by +1.

    If you hit the donut button, you correctly add the bonus, but as soon as you collect another donut, you just recalculate the total score based on the code snippet above, the program doesn't know that you gained a bonus.

    In order to not rescript everything for you, i'd recommend to simply add another variable which keeps track of the bonus score which you could simply add to the calculation in the code above.
     
    karammaks likes this.
  4. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    i appreciate you to read my post and reply , but how can i keep track of the bonus score ?
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    As i said, the simpliest way to implement it in your current code is putting another int variable into the first script. Set the default value to 0 and also add it to the calculation of the total score (that long piece of calculation i quoted in my previous post).
    This would look like
    Code (CSharp):
    1.     Score = score_donutBanana+score_donutChc+score_donutPink+score_sandwitchCake+
    2.                 score_jellyCake+score_donutPinkx+score_donutBananax+score_donutChcx+bonusScore;
    3.  
    In the second script, instead of assigning the bonus score to the 'scr' variable, do something like

    Code (CSharp):
    1. DestroyObjectAndAddPoint.bonusScore += DestroyObjectAndAddPoint.sumDonut * 2;
    .
    I just assume you'll set up just another static variable for that bonusScore anyways, so there you go. It's not the best way to do it, but it fits to your code and won't confuse you.