得分未重置为零

时间:2016-12-26 15:12:46

标签: c# unity5

我在比赛中获得分数和得分高的问题,当我玩游戏并吃硬币时,它会增加分数和高分但是当我死了并且再次玩时,它开始从我的分数开始计数,就像我死了一样当我在没有关闭比赛的情况下再次比赛时,我的得分为3分,从3开始计数,高分并没有保存

分数管理器脚本

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

public class ScoreManager : MonoBehaviour
{

    public Text scoreText;
    public Text hiScoreText;

    public static int scoreCount;
    public int hiScoreCount;

    public bool scoreIncreasing;



    // Use this for initialization
    void Start()
    {

        if (PlayerPrefs.HasKey("HighScore"))
        {
            hiScoreCount = PlayerPrefs.GetInt("HighScore");
        }
    }

    // Update is called once per frame
    void Update()
    { 

        if (scoreIncreasing)
        {
            scoreCount = BallMain.getPoints();
        }

        if (scoreCount > hiScoreCount)
        {
            hiScoreCount = scoreCount;
            PlayerPrefs.SetFloat("HighScore", hiScoreCount);
        }

        scoreText.text = "Score: " + (scoreCount);
        hiScoreText.text = "High Score: " + (hiScoreCount);
    }    
}

BallMain脚本

 using UnityEngine;
using System.Collections;

public static class BallMain  {

    private static float ballSpeed = 1.2f;
    private static int points;
    private static int lives = 0;

    public enum BallStateEnum { shielded,Vulnerable};

    public static BallStateEnum ballState = BallStateEnum.Vulnerable;

    public static float getBallSpeed()
    {
        return ballSpeed;
    }

    public static void IncreaseSpeed()
    {
        ballSpeed += 0.1f;
    }

    public static void IncreasePoints()
    {
        points++;

    }

   public static int getPoints()
    {
        return points;
    }

    public static int getLive()
    {
        return lives;
    }

    public static void addLife()
    {
        lives++;
    }
}

CoinHandler脚本

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

public class coinHandler : MonoBehaviour {

    public Transform particles;

    void OnCollisionEnter2D(Collision2D collider)
    {
        if (collider.gameObject.tag == "Player")
        {
            Instantiate(particles, new Vector3(transform.position.x, transform.position.y, -0.2f), Quaternion.identity);
            BallMain.IncreaseSpeed();
            BallMain.IncreasePoints();
            GameObject.FindWithTag("CoinUI").GetComponent<Text>().text = BallMain.getPoints().ToString();
            Destroy(gameObject);
        }
    }

BadCoinHandler脚本

  using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class BadCoinHandler : MonoBehaviour {

    public Transform particles;

    // Use this for initialization
    void Start ()
    {
        Destroy(gameObject, 8f);
    }

    void OnCollisionEnter2D(Collision2D collider)
    {
        if (collider.gameObject.tag == "Player")
        {
            Instantiate(particles, new Vector3(transform.position.x, transform.position.y, -0.2f), Quaternion.identity);
            Destroy(gameObject);

            if(BallMain.getLive() == 0 && BallMain.ballState == BallMain.BallStateEnum.Vulnerable)
            {
                SceneManager.LoadSceneAsync(0);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您必须手动将分数归零。因为您使用的是static关键字。因此,在 BallMain 中创建另一个空格,并使得零点;例如:

BigInteger

Score Manager脚本上的唤醒方法中调用它;例如:

public static void ResetPoints()
{
    points = 0;
}

要保存您的高分,请使用:

void Awake()
{
    scoreCount = 0; //Also the score here
    BallMain.ResetPoints(); 
}

顺便说一句,我还需要另一个空位来重置球速。