- Home /
Score counter breaking after adding points
I managed to make a score counter that adds 500 points when an object is destroyed. The destroy function only destroys the renderer and collider of the destructibles. However, upon activating the script to add 500 points when already at 500, instead of going to 1000, it goes to 0 and then to 500 again. The only way to get above 500 is to destroy several crates at once. Here is the script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreCounter : MonoBehaviour {
public Text countText;
private int score;
void Awake () {
score = 0;
SetCountText ();
}
void Update () {
}
void OnCollisionEnter (Collision coll) {
if (coll.relativeVelocity.magnitude > 25f && coll.other.tag == "Crate") {
score = score + 500;
SetCountText ();
}
}
public void SetCountText ()
{
countText.text = "Score: " + score.ToString ();
}
}
Why?! Please help, need an answer by tomorrow!
-EDIT- Now it doesn't give me any points at all. I think this whole problem is because it's attached to 41 objects at once, but I don't know how to work around that. I barely was able to do this script as is.
Because if I don't get one I have nothing to present tomorrow
$$anonymous$$aybe ins$$anonymous$$d of making score = 500 + score make 500 += score
The problem is because you have 41 objects in the screen which are all updating the same text field. You should only have 1 object in the scene which is keeping the score and is not interact-able with any of the objects.
Answer by Paricus · Dec 06, 2016 at 07:41 AM
Hi. If you get this in time, you need to create an empty game object by going to the create tab (in the hierarchy view) and selecting empty game object. then add the new score script to that empty game object, not the crates.
create a new script the name CrateScript, thats very important, and attach that script to your crates. now on the crate, you will see the script attached (in the inspector view) and a field saying Score Script, drag the empty game object (from the hierarchy view, not the scene) directly onto that field that says Score Script. Its probably i good idea to do that, then duplicate that crate a bunch of times, so you don't have to repeat the process of dragging the empty game object onto the Score Script field.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreCounter : MonoBehaviour {
public Text countText;
int score = 0;
public void SetCountText (int crateValue)
{
score += crateValue
countText.text = "Score: " + score.ToString ();
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CrateScript : MonoBehaviour {
public ScoreCounter scoreScript;
int crateValue = 500;
void OnCollisionEnter (Collision coll) {
if (coll.relativeVelocity.magnitude > 25f && coll.other.tag == "Crate") {
scoreScript.SetCountText(crateValue);
Destroy(col.gameObject, 2);
}
}
}
np, glad it works. Hope your presentation went well.
Answer by Commander-Rabbit · Dec 06, 2016 at 03:06 AM
maybe try
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreCounter : MonoBehaviour {
public Text countText;
private int score;
void Awake () {
score = 0;
SetCountText ();
}
void OnCollisionEnter (Collision coll) {
if (coll.relativeVelocity.magnitude > 25f && coll.other.tag == "Crate") {
500 += score;
SetCountText ();
}
}
public void SetCountText ()
{
countText.text = "Score: " + score.ToString ();
}
}