- Home /
The question is answered, right answer was accepted
Need Help! My Score Text won't updated until the last object collected
Hello Everyone, I got a problem which I tried to look for the solution but still can't get it... My game already finished but the score text won't updated until the last object (in my case it was a ring which ad +5 score) collected and the score would show the actual score... And the problem only appear when i tried it on my phone... When i tried at Unity the score would updated as soon as the player collide into the object but not on my phone... Here my code on the coin object.
public class CoinPickUp : MonoBehaviour {
public int score;
public Text scoretext;
public int setscore;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
public void Update ()
{
scoretext.text = ("Score: " + score);
setscore = score;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player") {
score += 5;
Destroy (gameObject);
}
}
}
and the i put the collider on my player for the ring
void OnTriggerEnter2D(Collider2D col)
{
if(col.tag == "FallDetector")
{
transform.position = respawnPoint;
}
if(col.tag == "Checkpoint")
{
respawnPoint = col.transform.position;
}
if(col.tag == "Ring")
{
GetComponent<AudioSource> ().Play ();
cp.score += 5;
Destroy (col.gameObject);
}
}
Answer by Trevdevs · Jun 28, 2017 at 08:14 AM
Judging by the line where you destroy the gameObject im assuming your placing this script on the pickup object itself which you really shouldn't do as once the object is destroyed that instance of the code is destroyed with it thus deleting the score. Not sure as to why it doesn't work on your phone may just be a phyx bug.
But some thing I would recommend you try and who knows may solve the issue entirely
Place one instance of the script on the player
Write this ;p normally i don't do this but there was quite a few things I would change lol
public class ObjectPickup : MonoBehaviour { public Text scoreText; private int score; void OnTriggerEnter2D(Collider2D col) { switch(col.tag) { case "FallDetector": transform.position = respawnPoint; break; case "CheckPoint": respawnPoint = col.transform.position; break; case "Ring": GetComponent<AudioSource> ().Play (); score += 5; scoreText.text = "Score: " + score; Destroy (col.gameObject); break; default: Debug.LogError ("Collided Object tag not set in switch statement"); //May want to remove this if it stops your game everytime break; } } }
Thanks friends, it worked well~ Guess the scripts attached to the ring make the trouble for the score to update and some misplaced code as well... Thanks once again ^^
Answer by Woltus · Jun 28, 2017 at 07:59 AM
Why do You update score text and score in different places? And why do You destroy coin two times?
To resolve your problem change coin script to something like that:
void UpdateScore()
{
score += 5;
scoretext.text = ("Score: " + score);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player") {
UpdateScore();
Destroy (gameObject);
}
}
I'm still beginner at program$$anonymous$$g and stuff so i followed few of tutorial and ended up in those scripts of $$anonymous$$e T T
And I have tried your code but it ended up make my score text appear only when i collider with the first coin and the score only stays at 5 no matter how many coin I collect... Don't know if i'm wrong or what...
Follow this Question
Related Questions
invoke a prefab as child 1 Answer
How to grab objects in 3rd person LITTLE BIG PLANET STYLE 2 Answers
Raycast/Non-Physics Collider Discrepancy 0 Answers
Object detection in front of Character 1 Answer
object name disappearing!! 1 Answer