【问题标题】:High Score not Working in unity高分不团结
【发布时间】:2017-04-02 16:56:00
【问题描述】:

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

输入的字符串格式不正确

这是我的代码:

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 ();
    }
}

【问题讨论】:

  • 好吧,大概ScorePoint.textHighScorePoint.text 不是整数的字符串表示形式。在不知道价值观是什么的情况下,很难再提供任何帮助......
  • 感谢您的回答

标签: c# unity3d


【解决方案1】:

在调用int.Parse之前确保HighScorePoint.text不为null或为空。

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();
    }
}

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

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

【讨论】:

  • 谢谢.....我找到了解决方案,这是您的回答第三次帮助我亲爱的程序员,祝您一切顺利
  • 很高兴我能提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
相关资源
最近更新 更多