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
public class HUD : MonoBehaviour
{
static Text scoretext;
static int actualscore = 0;
public static void blockhit()
{
block x = new block();
int y = x.blockhitpoint;
actualscore += y;
scoretext.text = "Score: " + actualscore.ToString();
}
void Start()
{
scoretext = GameObject.FindGameObjectWithTag("score").GetComponent<Text>();
scoretext.text = "Score: " + actualscore.ToString();
}
blockhit() method is called by another class block which is attached to each block.
public class block : MonoBehaviour
{
protected int blockpoint;
void OnCollisionEnter2D(Collision2D collision)
{
GameObject collider = collision.gameObject;
if (collider.CompareTag("Ball"))
{
HUD.blockhit();
Destroy(gameObject);
}
}
public int blockhitpoint
{
get
{ return blockpoint; }
}
blockpoint is assigned with value from child classes of block class.
public class StandardBlock : block
{ void Start()
{
blockpoint = 1;
}
score text is not updating in game. Any help would be appreciated!!