- Home /
How do i add points when the object past a specific coordinate
using UnityEngine;
using System.Collections;
public class score : MonoBehaviour {
private int scoreValue;
public GUIText guiScore;
private Transform Obstacle;
// Use this for initialization
void Start () {
Obstacle = transform;
scoreValue = 0;
guiScore.text = "Score: 0";
}
// Update is called once per frame
void Update () {
if (Obstacle.position.x <= -19.2 ){
scoreValue += 10;
guiScore.text = "Score: " + scoreValue;
this is what i have so far, it adds the points but instead of adding 10 everytime one of the obstacles past the x coordindate it keeps on adding ( think its becuz of the "<") so i need help in only adding 10 when it "equals" that specific coordinate
Answer by Chris_Dlala · May 28, 2014 at 07:49 PM
Hey, your condition in the update will be true every frame that that condition is met. If you only want the condition to be true once you need an additional check or to move / destroy the obstacle.
bool hasAwardedPoints = false;
void Update ()
{
if (Obstacle.position.x <= -19.2f && !hasAwardedPoints)
{
scoreValue += 10;
guiScore.text = "Score: " + scoreValue;
hasAwardedPoints = true;
}
}
or
void Update ()
{
if (Obstacle && Obstacle.position.x <= -19.2f)
{
scoreValue += 10;
guiScore.text = "Score: " + scoreValue;
Destroy(Obstacle.gameObject);
}
}
I hope that helps =)
Glad I could help! Could you accept my answer please. =)
ok im having a problem i created couple more obstacles, and when i attach the script to them ins$$anonymous$$d of adding to the score it starts it over from 10, how can i make it add to the already existing score ins$$anonymous$$d of restarting?
You don't really want each component as Score managing one Obstacle. It would be better to have just one score component (maybe a singleton) to access score. A quick fix would be to make the score variable static and not reset the score to 0 in start.
THAN$$anonymous$$ YOUUUU making the variable static truly help, thankssss againnnn, was fighting with this for a couple days, still a unity newbie, thankss