- Home /
Score System not working in Flappy Bird Clone
Hello,
I'm trying to make a Flappy Bird clone to improve myself.
I cannot make the score system to work, I'm a newbie in making games.
This is my Player script.
using UnityEngine;
public class Player : MonoBehaviour
{
public Vector2 jumpForce = new Vector2(0, 300);
public int score;
public GameObject obstacles;
public GUIText scoreText;
//Score Sytem
void Start ()
{
score = 0;
UpdateScore ();
}
void AddScore ()
{
score += 1;
UpdateScore ();
}
void UpdateScore () //Shows score on screen
{
scoreText.text = "Score : " + score;
}
void Update ()
{
// Keyboard
if (Input.GetKeyUp("space"))
{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpForce);
}
// Touchscreen
// Look for all fingers
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
// -- Tap: quick touch & release
// ------------------------------------------------
if (touch.phase == TouchPhase.Ended && touch.tapCount == 1)
{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpForce);
}
}
// Off screen die (NOT USED)
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
//ADDING POINTS
if (this.transform.position.x > obstacles.transform.position.x) {
AddScore ();
print ("Works!");
}
}
// Collision die
void OnCollisionEnter2D(Collision2D other)
{
Die();
}
void Die() //Back to the menu
{
Application.LoadLevel("menu");
}
}
The Player object has a fixed X and only moves on the Y axis. Obstacles move from right to left on the X axis. When Player Object's X is greater than Obstacle Object's X nothing happens.
How is that possible? What's wrong with my code?
I've been trying for days to fix this problem. You guys are my last hope.
Thanks in advance!
Ask me for any explanation.
I assume that "Works!" is not printed. Start by inserting at line 67:
Debug.Log(transform.position + ", " + obstacles.transform.position);
Answer by findujanvier · Jul 17, 2014 at 04:47 PM
your score variable is integer type. I should cast it to a string before you put it in your GUIText.
void UpdateScore () //Shows score on screen
{
scoreText.text = "Score : " + score.ToString ();
}
edit: umm i see an error ! that you haven't set up reference for scoreText.
void Start(){
textScore = gameObject.GetComponent<GUIText> ();
}
Answer by Kiwasi · Jul 17, 2014 at 07:18 PM
Where do you assign a value to obstacles? At the moment the code can only handle a single obstacle.
A better way to code this would be to have a trigger that fires every time an obstacle passes, and works from there.
An easier way to code this would be to simply add one point per second, the way flappy does.
I could also add a point for every obstacle created, but this won't help as it will just be a counter for the obstacles.
Neither adding a point every second will do the trick, as obstacles start to happear after a while :(
Can you give a plain English description of how your score system is supposed to work? I'm not sure we are on the same page here.
When Pleyer's X value is bigger than Obstacle's X value, a point is added to the score.
That's it.