void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == “flower”)
{
score += 1;
Debug.Log(score);
}
}
I have a projectile that when hits the collider of the flower enemy type it adds a point to the overall score. The Debug.Log does show the score increasing yet the display text in the canvas doesnt up date. any idea why that might be?
Here’s some screenshots of in game.
Hi
I don’t see (on your screenshots) which script is making the job of updating the UI.
You’ll need a script, like DataManager or GameManager, that will handle UI updating and data saving.
Here is a little example(not tested). Attach this script to an empty gameObject in your scene:
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public int score = 0;
public Text scoreText;
public void AddScore(int _score)
{
//updating our variable
score += _score;
//updating the UI text
scoreText.text = score.ToString();
}
}
And to your Flower.cs script add this changes:
//Attach the empty object with GameManager script to this variable in inspector
public GameManager GM;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "flower")
{
//adding score and updating UI
GM.AddScore(1);
Debug.Log(score);
}
}