高分不统一

时间:2017-04-02 16:56:01

标签: c# unity3d

我的游戏中有一个高分剧本。我在我的另一个游戏中使用相同的代码并且它可以工作但是在这个游戏中它给了我一个错误:

  

输入字符串的格式不正确

这是我的代码:

private Rigidbody2D Rigid;
public float Speed;
public float Power;
public GameObject Panel;
public Text ScorePoint;
private int Point = 0;
public Text HighScorePoint;

void Start()
{
    Rigid = GetComponent<Rigidbody2D> ();
    Panel.SetActive (false);
    HighScorePoint.text = PlayerPrefs.GetString ("Scores");
}

void Update()
{
    transform.Translate (Vector2.right * Speed * Time.deltaTime);

    foreach (Touch touch in Input.touches)
    {
        if (touch.phase == TouchPhase.Began)
        {
            Rigid.AddForce (Vector2.up * Power * Time.deltaTime);
        }
    }



// !!!!!!!!!!!!!!!!!! this is were it gives me an error !!!!!!!!!!!!!!!!!



    **if (int.Parse(ScorePoint.text) > int.Parse(HighScorePoint.text))**
    {
        PlayerPrefs.SetString ("Scores", ScorePoint.text);
        PlayerPrefs.Save ();
    }
}

void OnCollisionEnter2D(Collision2D Col)
{
    if (Col.gameObject.tag == "Pipe")
    {
        Panel.SetActive (true);
        Time.timeScale = 0;
    }
}

void OnTriggerEnter2D(Collider2D Col)
{
    if (Col.gameObject.tag == "ScoreCollider")
    {
        Point++;
        ScorePoint.text = Point.ToString ();
    }
}

我在其他游戏中使用此代码并且工作正常:

public float Speed;
private Rigidbody2D Rigid;
public float JumpPower;
public Text ScorePoint;
private int Point = 0;
public Text HighScorePoint;
public AudioSource CoinCollect;
public AudioSource Jump;
public AudioSource Laugh;
public AudioSource Scream;
private AudioSource[] Audios;

void Start()
{
    DontDestroyOnLoad (Scream);
    Audios = GetComponents<AudioSource> ();
    CoinCollect = Audios [0];
    Jump = Audios [1];
    Laugh = Audios [2];
    Scream = Audios [3];
    Rigid = GetComponent<Rigidbody2D> ();
    ScorePoint = GameObject.FindGameObjectWithTag ("ScorePoint").GetComponent<Text> ();
    HighScorePoint = GameObject.FindGameObjectWithTag ("HighScorePoint").GetComponent<Text> ();
    HighScorePoint.text = PlayerPrefs.GetString ("Scores");
}

void Update()
{
    transform.Translate (Vector2.right * Speed * Time.deltaTime);
    if (transform.position.y < -2.1)
    {
        foreach (Touch touch in Input.touches)
        {
            Rigid.AddForce (Vector2.up * JumpPower * Time.deltaTime);
            Jump.Play ();
        }
    }

    if (int.Parse(ScorePoint.text) > int.Parse(HighScorePoint.text))
    {
        PlayerPrefs.SetString ("Scores", ScorePoint.text);
        PlayerPrefs.Save ();
    }
}

void OnCollisionEnter2D(Collision2D Col)
{
    if (Col.gameObject.tag == "Enemies")
    {
        SceneManager.LoadScene ("CharacterSelection");
        Scream.Play ();
    }

    if (Col.gameObject.tag == "Coins")
    {
        Point++;
        ScorePoint.text = Point.ToString ();
        Destroy (Col.gameObject);
        CoinCollect.Play ();
    }

    if (Col.gameObject.tag == "Girls")
    {
        Point += 10;
        ScorePoint.text = Point.ToString ();
        Destroy (Col.gameObject);
        Laugh.Play ();
    }
}

1 个答案:

答案 0 :(得分:0)

在致电HighScorePoint.text之前,请确保int.Parse不为空或为空。

if (!String.IsNullOrEmpty(ScorePoint.text) && !String.IsNullOrEmpty(HighScorePoint.text))
{
    if (int.Parse(ScorePoint.text) > int.Parse(HighScorePoint.text))
    {
        PlayerPrefs.SetString("Scores", ScorePoint.text);
        PlayerPrefs.Save();
    }
}

您也可以使用TryParse并避免此类错误。

int tempScorePoint = 0;
int tempHighScorePoint = 0;

int.TryParse(ScorePoint.text, out tempScorePoint);
int.TryParse(HighScorePoint.text, out tempHighScorePoint);

if (int.TryParse(ScorePoint.text, out tempScorePoint) &&
    int.TryParse(HighScorePoint.text, out tempHighScorePoint))
{

    if (tempScorePoint > tempHighScorePoint)
    {
        PlayerPrefs.SetString("Scores", ScorePoint.text);
        PlayerPrefs.Save();
    }
}

通过在导致错误的代码之前插入Text,您可以轻松找到null组件Debug.Log

Debug.Log(ScorePoint.text);
Debug.Log(HighScorePoint.text);