简体   繁体   English

未分配的局部变量问题

[英]Unassigned local variable issue

I've been making an RTS game and I've just begun writing my code to define the base stats of certain units.我一直在制作 RTS 游戏,并且刚刚开始编写代码来定义某些单位的基本统计数据。 Everything in the code is correct, I've used the right namespaces and links and all that.代码中的所有内容都是正确的,我使用了正确的名称空间和链接等等。 One issue keeps coming up where the use of the term unit is unassigned.在未指定使用术语unit的情况下,一个问题不断出现。 I have assigned my BasicUnit term to defined my unit types ie infantry, workers etc. In my return function where I begin to type in the unit stats.我已经分配了我的 BasicUnit 术语来定义我的单位类型,即步兵、工人等。在我的返回 function 中,我开始输入单位统计信息。 It goes return (unit.cost, unit.attack) .return (unit.cost, unit.attack) For some reason the first unit term in this line is apparently unassigned.出于某种原因,这一行中的第一个unit术语显然未分配。 I use the term multiple times throughout the script and none of the have issues.我在整个脚本中多次使用该术语,并且没有任何问题。 But it is this line specifically that says that the first unit term is unassigned.但正是这一行特别说明了第一个unit项是未分配的。 It doesn't matter if there are 10 unit.stat or 1, the first one is always underlined.不管是 10 unit.stat还是 1,第一个总是带下划线的。 Please help me.请帮我。

namespace LP.FDG.Units
{
    public class UnitHandler : MonoBehaviour
    {

        public static UnitHandler instance;

        [SerializeField]
        private BasicUnit worker, infantry, archer, cavalry, priest;

        private void Start()
        {

            instance = this;

        }

        public (int cost, int attack, int health, int range, int size, int speed, int healing) GetBasicUnitStats(string type)
        {

            BasicUnit unit; // <--------
            switch (type)
            {
                case "worker":
                    unit = worker;
                    break;

                case "infantry":
                    unit = infantry;
                    break;

                case "priest":
                    unit = priest;
                    break;

                case "archer":
                    unit = archer;
                    break;

                case "cavalry":
                    unit = cavalry;
                    break;
            }

            **return (unit.cost, unit.attack, unit.healing, unit.health, unit.range, unit.speed, unit.size);**

        }

    public void SetBasicUnitStats(Transform type)
        {

            foreach(Transform child in type)
            {
                foreach (Transform unit in child)
                {

                    string unitName = child.name.Substring(0, child.name.Length - 1).ToLower();
                    var stats = GetBasicUnitStats(unitName);
                    Player.PlayerUnit pU;
                    
                    if(type == FDG.Player.PlayerManager.instance.playerUnits)
                    {
                        pU = unit.GetComponent<Player.PlayerUnit>();

                        pU.cost = stats.cost;
                        pU.attack = stats.attack;
                        pU.health = stats.health;
                        pU.healing = stats.healing;
                        pU.size = stats.size;
                        pU.speed = stats.speed;
                        pU.range = stats.range;
                    }
                    else if (type == FDG.Player.PlayerManager.instance.enemyUnits)
                    {

                    } 
                }
            }

        }

    }
}

The problem is that your switch statement doesn't guarantee that it will set unit to anything.问题是您的 switch 语句不能保证它将unit设置为任何东西。 For example as far as the compiler is concerned a value of "horsey" will not match anything and so unit is not assigned.例如,就编译器而言,“horsey”的值将不匹配任何内容,因此未分配单元。 Even though you might know that this will never be the case the compiler doesn't.即使可能知道编译器不会出现这种情况。

The way to deal with this is to add a default branch to your switch statement.处理这个问题的方法是在你的 switch 语句中添加一个default分支。 In this you can throw an ArgumentException saying that the type was not valid.在这种情况下,您可以抛出一个ArgumentException说明该类型无效。 It should never hit this default branch (if the rest of your code is right) but it will satisfy the compiler since it will no longer be possible to get to your return without having assigned unit (if you hit the default branch you are throwing an exception so not getting to the offending line).它永远不应该击中这个默认分支(如果你的代码的 rest 是正确的),但它会满足编译器,因为如果没有分配单元,它将不再可能到达你的返回(如果你击中默认分支,你正在抛出一个例外,所以不要进入违规行)。

The code would look something like this:代码看起来像这样:

switch (type)
{
    case "worker":
        unit = worker;
        break;
    /* All your other case statements still stay here */
    default:
        throw new ArgumentException($"Unrecognised unit type: {type}", "type");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM