Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

UI canvas not updating

Discussion in 'Scripting' started by TalhaJawed, Nov 21, 2019.

  1. TalhaJawed

    TalhaJawed

    Joined:
    Sep 18, 2019
    Posts:
    10
    Hello,
    I am trying to write a script for breakout game. I have a hud script which is attached to UI canvas. This script is used to update score of player. score text is not updating in game
    Code (CSharp):
    1. public class HUD : MonoBehaviour
    2. {
    3.    
    4.     static Text scoretext;
    5.     static int actualscore = 0;
    6.    
    7.  
    8.     public static void blockhit()
    9.     {
    10.         block x = new block();
    11.         int y = x.blockhitpoint;
    12.         actualscore += y;
    13.         scoretext.text = "Score: " + actualscore.ToString();
    14.  
    15.     }
    16.   void Start()
    17.     {
    18.         scoretext = GameObject.FindGameObjectWithTag("score").GetComponent<Text>();
    19.         scoretext.text = "Score: " + actualscore.ToString();
    20. }
    21.  
    blockhit() method is called by another class block which is attached to each block.
    Code (CSharp):
    1. public class block : MonoBehaviour
    2. {
    3.  
    4.    
    5.     protected int blockpoint;
    6.     void OnCollisionEnter2D(Collision2D collision)
    7.        
    8.     {
    9.         GameObject collider = collision.gameObject;
    10.         if (collider.CompareTag("Ball"))
    11.         {
    12.             HUD.blockhit();
    13.             Destroy(gameObject);
    14.            
    15.         }
    16.     }
    17.     public  int blockhitpoint
    18.     {
    19.         get
    20.         { return blockpoint; }
    21.        
    22.     }
    23.  
    blockpoint is assigned with value from child classes of block class.
    Code (CSharp):
    1. public class StandardBlock : block
    2. {    void Start()
    3.     {
    4.         blockpoint = 1;
    5.     }
    6.  
    score text is not updating in game. Any help would be appreciated!!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    You cannot use new to make a Monobehavior. See line 10 in your top script blob.

    Instead you can do any one of:

    - use .AddComponent<block>() to add it to an existing GameObject
    - use GetComponent<>() to find it on an existing GameObject
    - make a public inspector field and drag the reference in from another object in your scene or prefab.
     
  3. TalhaJawed

    TalhaJawed

    Joined:
    Sep 18, 2019
    Posts:
    10
    I have edit it as following:
    Code (CSharp):
    1. public class HUD : MonoBehaviour
    2. {
    3.  
    4.     static Text scoretext;
    5.     static int actualscore = 0;
    6.     static Text ballsleft;
    7.     static float actualballsleft = ConfigurationUtils.NumberOfBallsPerGame;
    8.     block x;
    9.  
    10.     public static void blockhit()
    11.     {
    12.      
    13.         int y = x.blockhitpoint;
    14.         actualscore += y;
    15.         scoretext.text = "Score: " + actualscore.ToString();
    16. }
    17.     void Start()
    18.     {
    19.         scoretext = GameObject.FindGameObjectWithTag("score").GetComponent<Text>();
    20.         scoretext.text = "Score: " + actualscore.ToString();
    21.         x = gameObject.AddComponent<block>();
    22.     }
    23.  
    its throwing error" of reference
    An object reference is required for the non-static field, method, or property 'HUD.x' "
    same is the case for getcomponent method
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    "static" means it's associated with the class, in abstract, rather than any particular copy of the class. If you are inside a static function and you want to access a non-static function or variable, you need to tell the computer which copy of the class you want to access, because there could be multiple copies (or zero) and the compiler doesn't know which one you want.
     
    Joe-Censored likes this.