Search Unity

Integer/Text Problem

Discussion in 'Scripting' started by Steelshot, Mar 3, 2017.

  1. Steelshot

    Steelshot

    Joined:
    Feb 24, 2015
    Posts:
    102
    Hi, my name is Pixel Steel but you can call me John.

    I have a minor bug in my game. There are three things that relate to this bug. One, the apple. Two, the integer that adds every time the player clicks an apple. And Three, the text display that shows the number of apples picked from the tree.

    The Bug:
    Whenever I start the game, the text states "Apples: 0", like it's suppose to. But the player has to click on an apple twice in order to change the text to "Apples: 1".

    What it's suppose to be:
    The "Apples: 0" text is suppose to add by 1 every time the player clicks on a apple.

    Code:
    Code (CSharp):
    1.     //For Picking an apple.
    2.     public Text applestxt;
    3.     public int Apples = 0;
    4.     //For Bakery.
    5.     public int Pies = 0;
    6.     public GameObject Bakery;
    7.     public Text piestxt;
    8.  
    9.     void Start () {
    10.         applestxt.text = "Apples: " + 0;
    11.         piestxt.text = "Pies: " + 0;
    12.     }
    13.  
    14.     void Update () {
    15.        
    16.         //This changes the text
    17.         txt.text = "Ray Distance: " + distance;
    18.  
    19.         //Basically declares where the ray is going
    20.         Vector3 forward = transform.TransformDirection (Vector3.forward) * distance;
    21.         Debug.DrawRay (transform.position, forward, Color.red);
    22.  
    23.         //Basically declaring Apples
    24.         if (Physics.Raycast (transform.position, (forward), out hit, 5.0f)) {
    25.             rayDistance = hit.distance;
    26.  
    27.             if (hit.collider.tag == ("Apple")) {
    28.  
    29.                 if (Input.GetKeyDown (KeyCode.Mouse0)) {
    30.                     applestxt.text = "Apples: " + Apples;
    31.                     Apples = Apples += 1;
    32.                 }    
    33.             }
    34.             //Declares what the Bakery will do.
    35.             //If tag is Bakery
    36.             if (hit.collider.tag == ("Bakery")) {
    37.                
    38.                 //When left mouse button is clicked
    39.                 if (Input.GetKeyDown (KeyCode.Mouse0)) {
    40.                    
    41.                     //Booleans, true or false?
    42.                     if (Apples >= 5) {
    43.                         Apples -= 5;
    44.                         print ("You Baked a Pie!");
    45.                     }
    46.                     else if (Apples <= 5) {
    47.                         print ("You Don't Have Enough Apples!");
    48.                         Pies += 1;
    49.                     }
    50.                 }
    51.             }
    52.                
    53.                
    54.             }
    55.         }
    56.     }
    57.  

    Any help is appreciated! :)
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Your setting your text before your increasing apples by 1:
    Code (CSharp):
    1.  if (hit.collider.tag == ("Apple")) {
    2.                 if (Input.GetKeyDown (KeyCode.Mouse0)) {
    3.                     applestxt.text = "Apples: " + Apples;
    4.                     Apples = Apples += 1;
    5.                 }  
    Move the apples + 1 above the text:
    Code (CSharp):
    1.  if (hit.collider.tag == ("Apple")) {
    2.                 if (Input.GetKeyDown (KeyCode.Mouse0)) {
    3.                     Apples = Apples += 1;
    4.                     applestxt.text = "Apples: " + Apples;
    5.                  
    6.                 }  
     
    Kurt-Dekker likes this.
  3. Steelshot

    Steelshot

    Joined:
    Feb 24, 2015
    Posts:
    102
    Thank you for this!
    I have been struggling with this bug ever since I have made those lines of code haha.
    Thank you!