RESETTING THE SCORE

Updated on April 27, 2018 in  [R] Scripts
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on April 27, 2018

hey guys i’m new to this field. i started with that dodging blocks tutorial in brackeys channel. so the thing here is i want to make some score kind of stuff but the problem is the score does not reset whenever the level restart here is my score code

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class ScoreScript : MonoBehaviour {
     public static int scoreValue = 0;
     Text score;
     // Use this for initialization
     void Start () {
         score = GetComponent<Text>();
     }
     
     // Update is called once per frame
     void Update () {
         score.text = scoreValue.ToString();
     }
     
 }

 

so basically the score gets called whenever the blocks spawn in the block spawner script

 

 void Update ()
     {
         if (Time.time >= timeToSpawn)
         {
             SpawnBlocks();
             ScoreScript.scoreValue += 100;
             timeToSpawn = Time.time + timeBetweenWaves;
         }

 

searched in google youtube didnt find any clear answer.

  • Liked by
Reply
3 on April 27, 2018

It’s because scoreValue is static. Static variables don’t get reset like normal ones do.

Try setting it to 0 in the start function.

 

edit: fixed wrong variable name.

on April 27, 2018

how do i refer to scoreValue in other scripts if its not static ? because block spawner script and score script r separated and i need to call the score in the block spawner in order to make score function. thats what i believed it could work if u’ve got better ideas than what i have though on just feel free to tell me 

Wise
on April 27, 2018

You can keep it static, you just need to properly reset it.

on April 27, 2018

thanks alot

Show more replies
  • Liked by
Reply
Cancel