• 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 merde10 · Jan 03, 2016 at 09:23 AM · gametimescore systempointsscoring

Associate time and distance for a score system

Hello, I introduce myself, my name is Samih.

I just start game development in unity.

I am currently developing a simple 2D game to learn many things about game development.

Nevertheless, I have some issues to create a score system.

Indeed, in the game, the score of the character is a function of the distance travelled (the scene and the character are static, only the textures move = treadmill effect).

So, I wanted to link the time spent by the player (before the game over) with distance.

when time increases, it hads values to the distance travelled.

(for example: for each seconds, it adds 5 points to distance travelled.)

I don't know how to do it

How can I do this?

Thank you.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SleeterSoulfire · Jan 03, 2016 at 11:10 AM

I'd be tempted to look into using Time.deltaTime to assess the time passed. I'd create a float to hold the value of your score and each Update add Time.deltaTime to the score variable.

The value can be multiplied by a score multiplier if desired. Something like:

 Public Class ClassName : MonoBehaviour {
     float score = 0;
     public float scoreMulti = 5;
 
     void Update() {
         score += Time.deltaTime * scoreMulti;
     }
 }
Comment
Add comment · 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
1

Answer by Priyanshu · Jan 03, 2016 at 11:44 AM

you can use coroutine to update score.

If u want to update score every second, just wait for 1 second and then add 1 to score. Which will mean player is running a distance of 1 unit every second.

You can increase speed of score by decreasing wait time. For example if u want to increase speed upto 2unit/s .Just wait for half a second. Following code updates score every second.

]] IEnumerator Score() {

   score++;
     // change score GUI
     yield return new WaitForSeconds(1f);
     }]]


Comment
Add comment · Show 5 · 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 merde10 · Jan 03, 2016 at 01:18 PM 0
Share

thanks for you reply, i will try it.

How can i display the score in game?

regards, Sam.

avatar image merde10 · Jan 03, 2016 at 05:45 PM 0
Share

thanks, i think it's much like this, but how can i show it on a UIText?

avatar image Priyanshu merde10 · Jan 04, 2016 at 03:13 PM 0
Share
 UIText.text = score.ToString();

put this code in coroutine.

avatar image merde10 Priyanshu · Jan 04, 2016 at 03:42 PM 0
Share

thanks, so i came to this script:

public class scoremanager : $$anonymous$$onoBehaviour {

void Start () {

public int score = 0

distance.text = score.ToString();

}

void Update () {

scoremanager++; yield return new Waitforseconds (1f);

} }

Is it okay for you?

Show more comments
avatar image
0

Answer by jimjames · Feb 14, 2016 at 06:42 PM

Why not just do something like this:

 public float time = 1;
 public float points = 5;
 
 private float startTime;
 private float endTime;
 
 private float score;
 
 void OnEnable() //or "Start"
 {
     startTime = Time.time;
 }
 
 void OnFinish()
 {
     endTime = Time.time;
 
     score = Mathf.RoundToInt(((endTime - startTime) / time) * points);
 
     print("player score = " + score);
 }

This way you save using "Update" or even a Coroutine.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

How to increase score by one per second when you are holding an object 2 Answers

How to make a timer read to the .001 of a second 2 Answers

Double Score Problem ? 1 Answer

Unity Game Score Script 1 Answer

counting score after destroying game object 1 Answer


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