• 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 /
  • Help Room /
avatar image
0
Question by TooLate · Dec 31, 2016 at 03:05 PM · timerscorescore systemaddscoreboard

How to add one score every second to scoremanager c#

Happy new year!

I got question regarding time and score. How do I add one score every second to my scoremanager. I got button which stops the time when pressed. After the button is being pressed I would like to see time as score (int) so that I can save it to my scoreboard. Here is my timer script.

using UnityEngine; using System.Collections; using UnityEngine.UI;

 public class TimerTimer: MonoBehaviour {

 public Text counterText;
 public float seconds, minutes;

     void Start (){
 counterText = GetComponent<Text> () as Text;

 }

 void Update(){

     minutes = (int)(Time.time/60f);
     seconds = (int)(Time.time % 60f);
     counterText.text = minutes.ToString("0") + ":" + seconds.ToString("0");
 }

     

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TooLate · Dec 31, 2016 at 10:05 PM

I searched around more and figured out that I need separate code to do this. I tried this but invoke method could´t be called. Any advice?

using UnityEngine; using System.Collections.Generic;

public class ScoreSecond : MonoBehaviour {

     private int now = 3; 
     private int score = 0; 
     public GUIText scoreLabel; 


     void Start(){
     scoreLabel.text = score.ToString();
     }

     void Update(){
         InvokeRepeating ("AddToScore", 01f, 02f);
         
     }

 void AdToScore(){
     if (now > 0) {
         score = score + 1; 
         scoreLabel.text = score.ToString();
     }
 }

}

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 yakri · Dec 31, 2016 at 10:15 PM 0
Share

01f is garbage, you want to say 'InvokeRepeating ("AddToScore", 1.0f, 1.0f);'

Also you misspelled your function name,` void AdToScore()` ins$$anonymous$$d of void AddToScore()

but you called the correctly spelled name (AddToScore) in the invoke. Since you have no function called AddToScore, it will not even try to work.

avatar image TooLate · Dec 31, 2016 at 10:38 PM 0
Share

I made the changes and the code works. Thank you $$anonymous$$r. Only problem is that the it dont give one point/second. If I wait for example 3 seconds the Guitext counter show 148. Any suggestions how to fix this?

avatar image TooLate · Jan 01, 2017 at 11:30 AM 0
Share

I found a way to do it. It works almost perfectly. There is just a slight delay at the beginning. Cheers!

using UnityEngine; using System.Collections;

public class ScorePoint : $$anonymous$$onoBehaviour {

 private IEnumerator coroutine;

 void Start()
 {

     coroutine = WaitAndPrint(1.0f);
     StartCoroutine(coroutine);

 }
     
 private IEnumerator WaitAndPrint(float waitTime) {
     while (true) {
         yield return new WaitForSeconds(waitTime);
         Score$$anonymous$$anager.score += 1;
     }
 }

}

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

88 People are following this question.

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

Related Questions

reload game 1 Answer

How do I add a score system? 1 Answer

Problem with counting score in 3D game 1 Answer

How do I make the Score system work? 1 Answer

Highscore table C# HELP!!! 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges