【问题标题】:ToString() doesn't workToString() 不起作用
【发布时间】:2015-12-01 12:58:16
【问题描述】:

我想获取 int 参数并将其保存到 playerprefs 然后获取它并将其更改为字符串并显示为字符串,但是当我使用 ToString();有错误:不能为 a=b 隐式转换类型 string' toint'; C# 统一 公共int a; 公共字符串 b;

 {
            car = GameObject.Find ("mastergame").GetComponent<master> ().carr;
            if (car == 1) {
                busted = GameObject.Find ("1").GetComponent<ruchauta1> ().busted;
                gameover = GameObject.Find ("1").GetComponent<ruchauta1> ().gameover;
                if (gameover == false && busted == false) {
                    scoree.text = "" + score;   
                }
                if (gameover == true || busted == true) {
                    if(score>bestscore){
                        bestscore=score;
                        PlayerPrefs.SetInt("bestscore",bestscore);}
                    score=0;
                    PlayerPrefs.SetInt("score",score);
                }               
            } else {
                busted = GameObject.Find ("Audi").GetComponent<ruchauta> ().busted;
                gameover = GameObject.Find ("Audi").GetComponent<ruchauta> ().gameover;
                if (gameover == false && busted == false) {
                    scoree.text = "" + score;

                }
                if (gameover == true || busted == true) {
                    if(score>bestscore){
                        bestscore=score;
                        PlayerPrefs.SetInt("bestscore",bestscore);
                        a=PlayerPrefs.GetInt("bestscore",bestscore);}
                    score=0;
                    PlayerPrefs.SetInt("score",score);              
                }
            }
            a.ToString ();
            a = b;
            bestscoree.text = b;
        }
        void Score(){
            car = GameObject.Find ("mastergame").GetComponent<master> ().carr;

            if (car == 1) {
                busted = GameObject.Find ("1").GetComponent<ruchauta1> ().busted;
                gameover = GameObject.Find ("1").GetComponent<ruchauta1> ().gameover;
                if (gameover == false && busted == false) {
                    score+=5;
                    PlayerPrefs.SetInt("score",score);              
                }

            } else {
                busted = GameObject.Find ("Audi").GetComponent<ruchauta> ().busted;
                gameover = GameObject.Find ("Audi").GetComponent<ruchauta> ().gameover;
                if (gameover == false && busted == false) {
                    score+=5;
                    PlayerPrefs.SetInt("score",score);
                }
            }

【问题讨论】:

  • 您在这里使用什么平台和语言库?在我看来可疑的是a.ToString ();,紧随其后的是a = b;。假设a.ToSTring (); 实际更改了a,那么所做的任何更改都将被a = b 覆盖。
  • 如果我删除 a=b,写 bestscoree.text = a;还是有这个错误。

标签: tostring


【解决方案1】:

ToString() 创建一个对象的字符串表示并返回该值。它不会(或不应该)改变它正在操作的对象的值/内容,当然也不会改变对象的类型。因此,a.ToString() 的行可能不会导致任何变化。

由于b 是一个字符串(基于您给出的错误)并且可能包含一个整数的字符串表示,如果您想将a 的值设置为b,您必须首先将b 转换为整数。我不确定这是什么语言,但这通常是用几种方法之一完成的。一些例子:

a = Convert.ToInt(b);
a = int.Parse(b);
a = Int32.Parse(b);

【讨论】:

    【解决方案2】:

    toString本身不会转换a的类型,你应该做的是:

    String newString = a.toString()
    

    之后,您就可以使用 newString 了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多