ToString()不起作用

时间:2015-09-05 13:47:16

标签: tostring

我想获取int参数并将其保存到playerprefs然后获取它并将其更改为字符串并显示为字符串,但是当我使用ToString()时;有错误:无法隐式转换类型string' to int'对于a = b; C#团结     public 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);
                }
            }

2 个答案:

答案 0 :(得分:0)

ToString()创建对象的字符串表示并返回该值。它不会(或不应该)更改它正在操作的对象的值/内容,它肯定不会更改对象的类型。因此a.ToString()行可能不会发生变化。

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

b

答案 1 :(得分:0)

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

String newString = a.toString()

之后,您可以使用newString。