- Home /
Problem in displaying the right text on screen
Hi I'm pretty new to this unity and I'm making a 2d game the problem I'm facing is displaying the right text in the screen , e.g I have a score
count for keeping track of score and then I'm displaying this text through count.text
, HOWEVER the problem is that when the game starts the text on screen displays "0", score
is 0 as well, then I shoot an apple and the score
becomes 1 as well as the count.text
also becomes 1 however the text is still 0 on the screen, when I shoot another arrow the score
and count.text
shows value of 2 however on the screen it shows 1 and so on. I followed unity tutorial of roll the ball. here is my code
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class arrowcounttutorial : MonoBehaviour {
public GameObject Arrow;
public GameObject apple;
public int score = 0;
public Text count;
// Use this for initialization
void Start () {
this.gameObject.GetComponent<Rigidbody2D> ().AddForce (transform.right*1500.0f);
//score = 0;
//showcounttext ();
count.text = score.ToString ();
}
// Update is called once per frame
void Update () {
Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
diff.Normalize();
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 0);
if (Input.GetMouseButtonUp (0)) {
GameObject bullet_new;
bullet_new = Instantiate (Arrow,new Vector2 (-0.23f, -3.78f), Quaternion.identity) as GameObject;
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector2.zero);
if (hit.collider!= null ) {
LeanTween.move(bullet_new, hit.collider.transform.localPosition, 1);
if(hit.collider.tag == "fruit")
{
score++;
//showcounttext();
count.text = score.ToString ();
print(count.text);
Destroy(hit.collider.gameObject,1);
Destroy(bullet_new,1);
}
}
}
}
/*
void showcounttext(){
count.text = score.ToString ();
}
*/
}
Also if I initialize the score in void start
the text remains 0 through out the level even if the score and count.text are 1,2,3,4,5 so on. What can I do?
Question.. Why are you doing "ToString();"s after the scores..?
You could just move the count.text = score; into Update before all of the raycasting/collider checking. That way it should constantly update.
Answer by TheFish657 · Jan 19, 2016 at 09:24 PM
Use Update instead of Start
using UnityEngine; using System.Collections; using UnityEngine.UI;
public GameObject Arrow; public GameObject apple; public int score = 0;
public Text count; // Use this for initialization void Start () { this.gameObject.GetComponent<Rigidbody2D> ().AddForce (transform.right*1500.0f);
//score = 0; //showcounttext (); }
// Update is called once per frame void Update () {
count.text = score.ToString (); Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; diff.Normalize();
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 0); if (Input.GetMouseButtonUp (0)) {
GameObject bullet_new; bullet_new = Instantiate (Arrow,new Vector2 (-0.23f, -3.78f), Quaternion.identity) as GameObject;
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector2.zero); if (hit.collider!= null ) { LeanTween.move(bullet_new, hit.collider.transform.localPosition, 1); if(hit.collider.tag == "fruit")
{ score++; //showcounttext(); count.text = score.ToString (); print(count.text);
Destroy(hit.collider.gameObject,1); Destroy(bullet_new,1); }
} }
}
}
Yes, he IS using Update. HOWEVER in the original code the "text" is only changed one time. When he puts it outside of the raycast/collider checking, that makes it so the text is changed to the score every frame.
Thanks for doing exactly what I said in my comment Fish. ;P
I think it would help if someone explained why this is expected to help. "Use Update ins$$anonymous$$d of Start" isn't actually what's going on here.
So can you either of you explain why this would make a difference? In the OP's code the text is being changed whenever the value of score
changes. $$anonymous$$ight that not be considered better practice than setting it every frame?
I guess, perhaps, if the issue was that score
was being changed elsewhere...
Your answer
![](https://koobas.hobune.stream/wayback/20220612060808im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
convert unity versions, 0 Answers
Issues after updating to Unity 5 1 Answer
Can't acces a variable from another script 1 Answer
How to draw a line in ARcore cloud anchor? 0 Answers
OnCollisionEnter2D dont work,OnCollisionEnter2D dont work 2 Answers