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 Dogg · Aug 12, 2014 at 01:04 AM · c#scorepausestop

Stop Score From Adding?

How can I stop my score from adding temporally for a certain amount of seconds? My score system uses Time, but I've had too many problems with using Time.timeScale = 0, so I would like an answer that doesn't involve that(unless it's the only way). So how can I do this? EDIT: I've made changes to the script, the new one posted is not the same as it was before. Just a heads up to the people who read this before. Here's my(New) script:

 float playerScore = 0;
     public Font MyFont;
     public bool On = true;
     public bool Off = false;
     ControllerScript player;
 
     void Start()
     {
         player = GetComponent<ControllerScript>();
     }
 
     void Update () {
       if(On == true && Off == false)
         playerScore += Time.deltaTime;
     }
 
     public void IncreaseScore(int amount)
     {
         playerScore += amount;
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         // If the colliding gameobject is Object...
         if(col.gameObject.tag == "Item1")
         {
             On = false;
             Off = true;
         }
 
     }
 
     void OnDisable()
     {
         PlayerPrefs.SetInt ("Score", (int)(playerScore * 5));
         PlayerPrefs.SetString("PlayerName", "High Score");
         PlayerPrefs.Save ();
         Debug.Log("Saved, Yes!");
     }
 
     void OnGUI()
     {
         GUI.skin.font = MyFont;
         GUI.contentColor = Color.white;
         GUI.Label (new Rect(10, 10, 200, 30), "Score: " + (int)(playerScore * 5));
     }
 }
 


All I really need to know is how to stop it, I can manage the seconds part.

Comment
Add comment · Show 4
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 prof · Aug 12, 2014 at 01:15 AM 0
Share

just use boolean val, switch it on/off when you need e.g if (addpoints) points++;

avatar image Dogg · Aug 12, 2014 at 02:44 AM 0
Share

Yeah this could work. I'll mess around and see what I can do.

avatar image Kiwasi · Aug 12, 2014 at 02:55 AM 0
Share

Using custom properties is another interesting option

avatar image Dogg · Aug 13, 2014 at 07:08 AM 0
Share

Sorry I don't know what "custom properties" are.

1 Reply

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

Answer by christoph_r · Aug 12, 2014 at 01:18 AM

There are numerous ways to do this. One would be to create a bool that needs to be true so the script adds to the score. Then create a method that simply sets that bool to true. Now if you want the scoring to stop, set the bool to false and invoke (one single time! So not in Update, or at least check if that bool isn't already true) the method that sets the bool to true again. When you invoke, you can set the amount of seconds you want to wait before the scoring continues again.

Another way would be using coroutines, but that requires some more reading to get into. It's a good idea to look at coroutines sooner or later, though, because they can be really powerful to use.

Comment
Add comment · Show 11 · 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 christoph_r · Aug 12, 2014 at 02:17 PM 1
Share

The scoring needs to stop upon collision or the not-scoring? If the first is the case, you can simply call the methd you created for temporarily stopping the scoring from another script with an OnTriggerEnter (or a similar) method. If it's the latter you're after, ins$$anonymous$$d of calling the wait method, you simply set the bool for scoring to true and cancel the invoke.

avatar image christoph_r · Aug 13, 2014 at 08:31 PM 1
Share

Okay, am I getting this right: The scoring continues again IF the player collides with the object again OR if the 3 seconds run out? If so: create the bool (I'll call it scoring for this post) as I said. Then create a method ResetBool that does nothing else except setting scoring to true. Then, in your`IncreaseScore` method, check if scoring, and only then add to the score.

In your OnTriggerEnter method, check if scoring is true. If so, set it to false and call Invoke("ResetBool", 3f);. Ins$$anonymous$$d of 3f, you can also use a variable resetTime, of course. In the corresponding else statement (that handles the case when the scoring already stopped and the player collides again), set the bool back to true and call CancelInvoke("ResetBool"); I hope that does the trick.

avatar image christoph_r · Aug 13, 2014 at 11:32 PM 1
Share

Alright, let me know if it works!

avatar image christoph_r · Aug 14, 2014 at 12:15 AM 1
Share

Cool. Glad you managed to figure that out on your own, bug tracking can be tricky business! In that case, make sure you've set everything up, that both objects have colliders and at least one is a trigger. The documentation also has a useful chart showing when OnTriggerEnter works.

avatar image christoph_r · Aug 14, 2014 at 01:05 PM 1
Share

That depends on the setup of your objects. Here is a nice overview about what method will be called in which situations.

Show more comments

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

24 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Guitext for score over time. 2 Answers

What am I doing wrong with PlayerPrefs? 3 Answers

how to setting highscore with second in unity 2d? (C#) 1 Answer

Dramatic Camera effect with all physical gameobjects paused? 0 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