Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Squi_Beta · Jul 17, 2014 at 03:53 PM · not workingscoresystem

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jul 17, 2014 at 04:29 PM 1
Share

I assume that "Works!" is not printed. Start by inserting at line 67:

  Debug.Log(transform.position + ", " + obstacles.transform.position);

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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> ();
 }
 


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Squi_Beta · Jul 17, 2014 at 07:12 PM 0
Share

Thanks for your answer, but it's still not working :(

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Squi_Beta · Jul 17, 2014 at 07:46 PM 0
Share

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 :(

avatar image Kiwasi · Jul 17, 2014 at 09:20 PM 0
Share

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.

avatar image Squi_Beta · Jul 18, 2014 at 11:22 AM 0
Share

When Pleyer's X value is bigger than Obstacle's X value, a point is added to the score.

That's it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need help making my score not reset on Awake, but on a scene i have 0 Answers

I Need help keeping my score forever 1 Answer

¿Score system with the new UI? 0 Answers

How do I get a component of canvas? 0 Answers

Point system doesnt add points after 10 seconds 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges