获取错误字段`BaseStats.baseStats.hp'必须在控制离开构造函数之前完全分配

时间:2013-04-14 09:40:16

标签: c# unity3d

我试图解决这个问题但未能如此实现。无法将int类型隐式转换为BaseStats.Stat

using UnityEngine;
using System.Collections;

public class BaseStats : MonoBehaviour {

    public struct baseStats {
    public string name;
    public int level;
    public Stat hp;
    public int ap;

    public int strength;
    public int toughness;
    public int agility;
    public int intelligence;
    public int willPower;
    public int luck;

    public int attack;
    public int hitPercentage;
    public int defence;
    public int evasionPercentage;
    public int abilityAttack;
    public int abilityDefence;
    public int abilityDefencePercentage;

    public int exp;

        public baseStats(string Name, int Level, int Hp, int Ap, int Strength, int Toughness, int Agility, int Intelligence, int WillPower, int Luck, int Attack, int HitPercentage, int Defence, int EvasionPercentage, int AbilityAttack, int AbilityDefence, int AbilityDefencePercentage, int Exp) {
            name = Name;
            level = Level;
            hp = Hp;
            ap = Ap;

            strength = Strength;
            toughness = Toughness;
            agility = Agility;
            intelligence = Intelligence;
            willPower = WillPower;
            luck = Luck;

            attack = Attack;
            hitPercentage = HitPercentage;
            defence = Defence;
            evasionPercentage = EvasionPercentage;
            abilityAttack = AbilityAttack;
            abilityDefence = AbilityDefence;
            abilityDefencePercentage = AbilityDefencePercentage;

            exp = Exp;

        }
    }

    public class Stat {
        int current;
        int max;
    }

    void Start() {

        baseStats mainChar = new baseStats( 
            "Truth",
            99,
            9999,
            999,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            100,
            255,
            255, 
            100,
            7777777);

        print(mainChar.level);

    }
}

我正在努力让HP拥有最新版本和最大版本,这就是教程教我的方式。不幸的是,教程是在unityscript中,而我想在c#和

中进行编码

3 个答案:

答案 0 :(得分:2)

在您指定的构造函数中:

hp = Hp;

您的参数HP的类型为int,并且您已将字段hp定义为Stat

public Stat hp;

这就是您收到错误的原因,我相信您也希望将您的字段定义为int。另外作为旁注,如果您按照.Net naming conventions

更好

答案 1 :(得分:0)

问题是您通过构造函数传递intHp)并将其分配给Stathp)类型的变量:

hp = Hp;

将构造函数更改为:

public baseStats(string Name, int Level, Stat Hp /*...*/){
    //...
}

修改

  

参数#3'无法将int'表达式转换为类型BaseStats.Stat'   和最好的重载方法匹配   BaseStats.baseStats.baseStats(string,int,BaseStats.Stat,int,int,   int,int,int,int,int,int,int,int,int,int,int,int,int)'has   一些无效的论点

你的第二个问题是更改构造函数你还必须将正确的对象传递给构造函数:

baseStats mainChar = new baseStats( 
            "Truth",
            99,
            9999, //It's an int and you have to pass an object of type Stat
            999,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            100,
            255,
            255, 
            100,
            7777777);

将其更改为:

Stat myStat = new Stat(); //create the object needed

baseStats mainChar = new baseStats( 
            "Truth",
            99,
            myStat,  //pass it!
            999,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            100,
            255,
            255, 
            100,
            7777777);

答案 2 :(得分:0)

首先Stat应该是Enum根据您提供的用法而不是class -

public enum Stat
{
   int current;
   int max;
}

这是类型转换的问题。将参数传递为Enum(Stat) -

public baseStats(string Name, int Level, Stat Hp, ....)

或者在分配之前将其强制转换为枚举(Stat) -

hp = (Stat)Hp;