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 SUPPEAR · Mar 30, 2015 at 01:25 AM · android2dmobile

Reset Score after game is finished?

I am trying to set the score of the game to zero every time the game is reset, I have no clue how to do this. I have two scripts that are for the score.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ScoreManager : MonoBehaviour
 {
     public static int score;        // The player's score.
     
     
     Text text;                      // Reference to the Text component.
     
     
     void Awake ()
     {
         // Set up the reference.
         text = GetComponent <Text> ();
 
     }
     
     
     void Update ()
     {
         // Set the displayed text to be the word "Score" followed by the score value.
         text.text = "Score: " + score;
     }
 }


 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
     
     public int scoreValue = 1;
     // Update is called once per frame
     void OnMouseDown () {
     
         ScoreManager.score += scoreValue;
 
     }
 }
 

The first one is to display the score. The second one adds points when the object is touched. After 30 seconds the game takes you to a menu and displays the score. Then there is a reset button that when is touched, I want it to reset the score. How do I do this.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hostShadows · Mar 30, 2015 at 05:47 AM

make an if statement in the Update that will set score = 0; when it's called (it should be called when the reset button is clicked) http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseUp.html this is what is used to detect clicking on an object

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
0

Answer by VioKyma · Mar 30, 2015 at 01:56 AM

If you are using Application.LoadLevel() on your Reset button, just set the score to 0 on Awake.

 void Awake ()
 {
     // Set up the reference.
     text = GetComponent <Text> ();
     score = 0;
 }

If not (or if you have set DontDestroyOnLoad on the ScoreManager), you will just need to get a reference to the ScoreManager and set the score to 0 on the Reset button click.

Comment
Add comment · Show 7 · 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 SUPPEAR · Mar 30, 2015 at 02:12 AM 0
Share

The reason I don't use void Awake is because it would only the show the score whilst you are playing it. I want the score to be shown after the game is done. ( I would like to note I am new to scripting ) I have made this script hoping it would just set the score to 0, but it doesn't, which is why I posted.

 using UnityEngine;
 using System.Collections;
 
 public class Law : $$anonymous$$onoBehaviour {
     
 
     void On$$anonymous$$ouseDown(){
         Score$$anonymous$$anager.score = 0;
     }
 }
avatar image VioKyma · Mar 30, 2015 at 02:18 AM 0
Share

Okay, you will need to get the correct 'instance' of the Score$$anonymous$$anager script before you can set the score.

If the Score$$anonymous$$anager is a part of the same game object as Law, then you can use GetComponent():

 void On$$anonymous$$ouseDown(){
     Score$$anonymous$$anager score$$anonymous$$anager = GetComponent<Score$$anonymous$$anager>();
     score$$anonymous$$anager.score = 0;
 }

Otherwise, you will need to find the game object the Score$$anonymous$$anager is on, then get the component and set it (as above). If you need, I can help with getting the game object, just let me know how it's setup :)

avatar image SUPPEAR · Mar 30, 2015 at 03:21 AM 0
Share

The Score$$anonymous$$anager script is on a different object, the name of the object is ScoreText ( It is a UI text renamed, if that makes any difference ) The Law script is on a UI Button that has the script

 #pragma strict
 
 function LoadLevel(){
     Application.LoadLevel("s");
     }
     
 

I used an event trigger to have this script change the level.

avatar image VioKyma · Mar 30, 2015 at 04:12 AM 0
Share

Okay, not 100% on that last script you posted, but I think you can either do:

 void On$$anonymous$$ouseDown(){
     Score$$anonymous$$anager score$$anonymous$$anager = 
         GameObject.Find("ScoreText").GetComponent<Score$$anonymous$$anager>();
     score$$anonymous$$anager.score = 0;
 }

Or in the LoadLevel function:

 function LoadLevel(){
     Score$$anonymous$$anager score$$anonymous$$anager = 
         GameObject.Find("ScoreText").GetComponent<Score$$anonymous$$anager>();
     score$$anonymous$$anager.score = 0;
     Application.LoadLevel("s");
 }
avatar image SUPPEAR · Apr 03, 2015 at 07:29 PM 0
Share

I decided to use the first one you mentioned. Using the exact same script this error pops up

Assets/Standard Assets ($$anonymous$$obile)/Textures/Law.cs(9,30): error CS0176: Static member `Score$$anonymous$$anager.score' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d

So I changed the script to

 using UnityEngine;
 using System.Collections;
 
 public class Law : $$anonymous$$onoBehaviour {
 
     void On$$anonymous$$ouseDown(){
             GameObject.Find("ScoreText").GetComponent<Score$$anonymous$$anager>();
         Score$$anonymous$$anager.score = 0;
     }
 }

But it doesn't set the score to 0

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

23 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

Related Questions

Expand object on touch? 1 Answer

While the Parent expands, the children get smaller. Problem 2 Answers

Avoid touch input when pressing the "Pause" button 1 Answer

Destroy Object On Touch? 2D 1 Answer

Importing Facebook SDK on project makes the build crash on Android 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