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): //For Picking an apple. public Text applestxt; public int Apples = 0; //For Bakery. public int Pies = 0; public GameObject Bakery; public Text piestxt; void Start () { applestxt.text = "Apples: " + 0; piestxt.text = "Pies: " + 0; } void Update () { //This changes the text txt.text = "Ray Distance: " + distance; //Basically declares where the ray is going Vector3 forward = transform.TransformDirection (Vector3.forward) * distance; Debug.DrawRay (transform.position, forward, Color.red); //Basically declaring Apples if (Physics.Raycast (transform.position, (forward), out hit, 5.0f)) { rayDistance = hit.distance; if (hit.collider.tag == ("Apple")) { if (Input.GetKeyDown (KeyCode.Mouse0)) { applestxt.text = "Apples: " + Apples; Apples = Apples += 1; } } //Declares what the Bakery will do. //If tag is Bakery if (hit.collider.tag == ("Bakery")) { //When left mouse button is clicked if (Input.GetKeyDown (KeyCode.Mouse0)) { //Booleans, true or false? if (Apples >= 5) { Apples -= 5; print ("You Baked a Pie!"); } else if (Apples <= 5) { print ("You Don't Have Enough Apples!"); Pies += 1; } } } } } } Any help is appreciated!
Your setting your text before your increasing apples by 1: Code (CSharp): if (hit.collider.tag == ("Apple")) { if (Input.GetKeyDown (KeyCode.Mouse0)) { applestxt.text = "Apples: " + Apples; Apples = Apples += 1; } Move the apples + 1 above the text: Code (CSharp): if (hit.collider.tag == ("Apple")) { if (Input.GetKeyDown (KeyCode.Mouse0)) { Apples = Apples += 1; applestxt.text = "Apples: " + Apples; }
Thank you for this! I have been struggling with this bug ever since I have made those lines of code haha. Thank you!