Search Unity

[SOLVED] UI Text not changing after onClick function.

Discussion in 'UGUI & TextMesh Pro' started by Zewde, Dec 17, 2015.

  1. Zewde

    Zewde

    Joined:
    Nov 30, 2015
    Posts:
    3
    Hey guys,

    I have a UI Text called Total Points Text in CANVAS 1 and an Image in CANVAS 2.
    A button component is attached to the Image so that when it's touched, the image gets destroyed and should change the text in Total Points Text.

    The problem is, the function which is called onClick of Image is called but the Text from Total Points Text isn't changing.

    The weird part is, when I move the line which changes the Text to Start() of Image, the Text does change...
    Another weird thing is that I do change the Total Points Text a couple of times with my aim script (the one attached to my character) and it works fine. Maybe the problem is coming from the button-onClick?

    Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class twohundredPTSScript : MonoBehaviour
    6. {
    7.     public Image TWOHundredpointsImage;
    8.     public Text TotalPointsText;
    9.  
    10.  
    11.    
    12.    
    13.     public void Purchase200()
    14.     {
    15.          int totalpoints = ballscript.TotalPoints;
    16.  
    17.         if(totalpoints >= 200)
    18.         {
    19.             Destroy(TWOHundredpointsImage); // CALLED
    20.             totalpoints = totalpoints - 200; // CALLED
    21.             Debug.Log(totalpoints); // CALLED
    22.             Debug.Log("Item bought !"); // CALLED
    23.  
    24.             TotalPointsText.text = "Total points: " + totalpoints.ToString(); // THIS ISN'T CALLED OR ISN'T WORKING.
    25.         }
    26.  
    27.         else
    28.         {
    29.             Debug.Log("Not enough points !");
    30.         }
    31.  
    32.         ballscript.TotalPoints = totalpoints;
    33.     }
    34. }
    35.