【问题标题】:Can't find syntax error anywhere in C# script在 C# 脚本的任何地方都找不到语法错误
【发布时间】:2021-04-23 07:36:41
【问题描述】:

我一直在开发一个统一管理“单位”的 C# 脚本,简而言之,编译器告诉我“(”在我的整数函数中是意外的 - 下面粘贴代码的第 20 行 - 在 'int cost 之前' 而且我在任何地方都找不到我的脚本的问题。如果我删除整个函数,则没有错误并且它编译成功,所以我 99% 确定问题出在函数本身。为缩进道歉,新到 StackOverflow 并且不擅长在此处添加代码!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Iska.CNC.Units
{

public class UnitHandler : MonoBehaviour 
{
    public static UnitHandler instance;

    [SerializeField]
    private BasicUnit Pitbull, RaiderBuggy;

    private void Start()
    {
        instance = this;
    }

    public (int cost, int attack, int attackRange, int health, int tier) GetBasicUnitStats(string type)
    {
        BasicUnit unit;
        switch (type)
        {
            case "Pitbull":
                unit = Pitbull;
                break;
            case "RaiderBuggy":
                unit = RaiderBuggy;
                break;
            default:
                Debug.Log("Error - unit cannot be found or does not exist");
                return (0, 0, 0 ,0 ,0);
        }
        return (unit.cost, unit.attack, unit.attackRange, unit.health, unit.tier);
    }

    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 == Iska.CNC.Player.PlayerManager.instance.playerUnits) 
                {
                    pU = unit.GetComponent<Player.PlayerUnit> ();
                    //set unit stats in each unit
                    pU.cost = stats.cost;
                    pU.attack = stats.attack;
                    pU.attackRange = stats.attackRange;
                    pU.health = stats.health;
                    pU.tier = stats.tier;
                } 
                else if (type == Iska.CNC.Player.PlayerManager.instance.enemyUnits) 
                {
                    //set enemy stats
                }
                //if we have any upgrades or buffs, add them now
                //add upgrades to unit stats
            }
        }
    }

}
}

【问题讨论】:

标签: c# unity3d compiler-errors


【解决方案1】:

您可以使用替代方法

public Tuple<int, int, int, int> GetBasicUnitStats (string type)
{
    BasicUnit unit;
    switch (type)
    {
        case "Pitbull":
            unit = Pitbull;
            break;
        case "RaiderBuggy":
            unit = RaiderBuggy;
            break;
        default:
            Debug.Log("Error - unit cannot be found or does not exist");
            return Tuple.Create(0, 0, 0 ,0 ,0);
    }

    return Tuple.Create(unit.cost, unit.attack, unit.attackRange, unit.health, unit.tier);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多