简体   繁体   中英

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. 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. 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. It goes return (unit.cost, unit.attack) . For some reason the first unit term in this line is apparently unassigned. 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. It doesn't matter if there are 10 unit.stat or 1, the first one is always underlined. 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. For example as far as the compiler is concerned a value of "horsey" will not match anything and so unit is not assigned. 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. In this you can throw an ArgumentException saying that the type was not valid. 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).

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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