Search Unity

[SOLVED] Updating Text that has been initiated (completely stuck)

Discussion in '2D' started by CptBubbleButt, Apr 26, 2019.

  1. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    So one of the main mechanics in my game is blocks having assigned points, which the player has to destroy to progress through the level.

    I have everything working apart from two things: The text is not updating when the block is damaged and the text is not getting destroyed with the block. I think if I find out how to do one, the second will be fixed. As this is a main mechanic, I need this to work before I can progress with the game.

    Script that is attached to the blocks:

    Code (CSharp):
    1. public class PointBlock : MonoBehaviour
    2. {
    3.     public GameObject TextPrefab;
    4.     public RectTransform canvasTransform;
    5.  
    6.     ControlText controlText;
    7.  
    8.     public Text pointText;
    9.  
    10.     public int pointNum;
    11.  
    12.     public int minNumber;
    13.     public int maxNumber;
    14.  
    15.     private void Awake()
    16.     {
    17.         pointNum = Random.Range(minNumber, maxNumber);
    18.        
    19.         Vector3 pos = this.transform.position;
    20.         CreateText(pos);
    21.        
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.        
    27.     }
    28.  
    29.  
    30.     void CreateText(Vector3 _pos)
    31.     {
    32.         InitializeText();
    33.      
    34.         GameObject instance = Instantiate(TextPrefab, _pos, Quaternion.identity);
    35.  
    36.         instance.transform.SetParent(canvasTransform);
    37.         instance.transform.localScale = new Vector3(1, 1, 1);
    38.  
    39.         controlText = instance.transform.GetComponent<ControlText>();
    40.      
    41.     }
    42.  
    43.     public void TakeDamage(int _damage)
    44.     {
    45.        
    46.         if (pointNum == 0)
    47.         {
    48.             Destroy(this.gameObject);
    49.         }
    50.         else
    51.         {
    52.             pointNum -= _damage;
    53.             controlText.CheckText(pointText, pointNum);
    54.         }
    55.  
    56.  
    57.     }
    58.  
    59.     public void InitializeText()
    60.     {
    61.      
    62.         pointText.text = TextPrefab.transform.GetComponent<Text>().text;
    63.         pointText.text = "" + pointNum;
    64.  
    65.        
    66.     }
    67.  
    68.  
    69. }
    Where the TakeDamage function is (attached to the bullet):

    Code (CSharp):
    1.      if (hitInfo.CompareTag("Point_Block"))
    2.         {
    3.             PointBlock numOfPoints = hitInfo.GetComponent<PointBlock>();
    4.  
    5.             numOfPoints.TakeDamage(damage);
    6.            
    7.  
    8.             Destroy(this.gameObject);
    9.         }
    10.     }
    Sorry for the long read, it is probably a simple fix but I can't see it.

    Thank you for any help given,


    - J
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    So you have an object here that dynamically spawns a Text object at the objects position.

    I'm assuming ControlText is responsible for managing the text there (it might be good to post that script as well).

    I think you should either have ControlText be solely responsible for managing the Text component, or get rid of ControlText and do it all inside this class.

    For the sake of a clear explanation, I just wrote up an example of what I think you're going for (correct me if not):
    Code (CSharp):
    1. public class PointBlock : MonoBehaviour
    2. {
    3.     public GameObject TextPrefab;
    4.     public RectTransform canvasTransform;
    5.     public int minNumber;
    6.     public int maxNumber;
    7.  
    8.     public int CurrentPoints { get; private set; }
    9.  
    10.     private ControlText controlText;
    11.  
    12.     private void Awake()
    13.     {
    14.         CurrentPoints = Random.Range(minNumber, maxNumber);
    15.  
    16.         CreateText();
    17.         UpdateText();
    18.     }
    19.  
    20.     private void CreateText()
    21.     {
    22.         GameObject instance = Instantiate(TextPrefab, transform.position, Quaternion.identity);
    23.  
    24.         instance.transform.SetParent(canvasTransform);
    25.         instance.transform.localScale = Vector3.one;
    26.  
    27.         controlText = instance.GetComponent<ControlText>();
    28.     }
    29.  
    30.     public void TakeDamage(int _damage)
    31.     {
    32.         // reduce points but dont let it go below 0
    33.         CurrentPoints = Mathf.Max(0, CurrentPoints - _damage);
    34.  
    35.         UpdateText();
    36.  
    37.         if (CurrentPoints == 0)
    38.         {
    39.             // this destroys the PointBlock.  If the canvas and/or ControlText object
    40.             // is not a child of this object, you will need to destroy it separately.
    41.             Destroy(gameObject);
    42.             Destroy(controlText.gameObject);
    43.         }
    44.     }
    45.  
    46.     private void UpdateText()
    47.     {
    48.         controlText.SetText(CurrentPoints.ToString());
    49.     }
    50. }
    Code (CSharp):
    1. public class ControlText : MonoBehaviour
    2. {
    3.     public Text text; // set in the inspector for this prefab
    4.  
    5.     public void SetText(string newText)
    6.     {
    7.         text.text = newText;
    8.     }
    9. }
    I think it simplifies things a lot when you give objects specific responsibilities. In this case, I'm letting ControlText own and manage the Text component. The PointBlock object is responsible for managing its points, and then it tells the ControlText to update and passes the current points value.

    If ControlText doesn't really do anything else, you could also delete that component, get Text component instead and set the text directly in PointBlock.

    Hopefully this makes sense!
     
    Last edited: Apr 26, 2019
  3. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    Thank you! That does make a lot of sense, I didn't know how I was going to use the ControlText so at the moment it was a blank (ish) script. You have moved me into the right direction, once again thank you!
     
    LiterallyJeff likes this.
  4. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    Here is what the game looks like with the help of your code:
     
    LiterallyJeff likes this.