Reputation: 1
I'm designing a game for my project and somehow the score(text) does not update after an action. It stuck at 0.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class uiManager : MonoBehaviour {
public Text scoreText;
bool gameOver;
int score;
// Use this for initialization
void Start () {
gameOver = false;
score = 0;
InvokeRepeating ("scoreUpdate", 1.0f, 0.5f);
}
// Update is called once per frame
void Update ()
{
scoreText.text = "Point: " + score;
}
void scoreUpdate()
{
if (gameOver == false)
{
score += 1;
}
}
public void gameOVER()
{
gameOver = true;
}
public void Play()
{
Application.LoadLevel ("MuachiJump");
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "branch")
{
score += 1;
Destroy(col.gameObject);
}
}
I just want to make sure is there any mistake in this code? All of them seem to be correct.
Upvotes: 0
Views: 4302
Reputation: 11
In my case the size of the textbox was the issue, just enlarge it and your score will be visible.
Upvotes: 0
Reputation: 111
Do you have an active object with the script attached on your scene?
Upvotes: 0
Reputation: 6693
The code by itself seems fine. Make sure that uiManager
is attached to an object in your scene that is active. In the Update
method, if you add, for example, Debug.Log(score)
, it should print to the log every frame. If this isn't happening, you need to attach the script to an object in your scene as well as make sure that the Text object has a valid reference.
Upvotes: 3