Unity3D - 错误 - 无法将类型'int'隐式转换为'string'

时间:2014-12-06 01:31:27

标签: c# unity3d

我的代码列在这里,这是一个RPG游戏的编码,但是在帮我解决这个问题时无所谓。这是控制台一直给我的唯一问题:

  

资产/标准资产/脚本/ Base Player / BasePlayer.cs(20,21):错误CS0029:无法隐式转换类型' int'到'字符串'

代码:

using UnityEngine;
using System.Collections;

public class BasePlayer {

    private string playerName;
    private int playerLevel;
    private BaseCharacterClass playerClass;
    private int speed;
    private int endurance;
    private int strength;
    private int health;

    public string PlayerName{
        get{return playerName;}
        set{playerName = value;}
    }
    public int PlayerLevel{
        get{return playerLevel;}
        set{playerName = value;}
    }
    public BaseCharacterClass PlayerClass{
        get{return playerClass;}
        set{playerClass = value;}
    }
    public int Speed{
        get{return speed;}
        set{speed = value;}
    }
    public int Endurance{
        get{return endurance;}
        set{endurance = value;}
    }
    public int Strength{
        get{return strength;}
        set{strength = value;}
    }
    public int Health{
        get{return health;}
        set{health = value;}
    }
} 

1 个答案:

答案 0 :(得分:1)

这是你的错误

public int PlayerLevel{
    get{return playerLevel;}
    set{playerName = value;}
}

您尝试在设置器中为playerName分配一个整数,但playerName是一个字符串,因此您收到了错误消息。它应该改为

public int PlayerLevel{
    get{ return playerLevel; }
    set{ playerLevel = value; }
}

假设BasePlayer类仅包含简单属性,您还可以使用自动实现的属性简化代码,如下所示

public class BasePlayer 
{
    public string PlayerName { get; set; }
    public int PlayerLevel { get; set; }
    public BaseCharacterClass PlayerClass { get; set; }
    public int Speed { get; set; }
    public int Endurance { get; set; }
    public int Strength { get; set; }
    public int Health { get; set; }
}