Can';在C#脚本中找不到语法错误
我一直在用unity编写一个控制“单位”的C#脚本,简而言之,编译器告诉我('在我的整数函数中是意外的-下面粘贴的代码中的第20行-在'int cost'之前,我在任何地方都找不到我的脚本的问题。如果我删除整个函数,没有错误,并且它成功编译,因此我99%确定问题在于函数本身。对于缩进,我深表歉意,这是StackOverflow和bad的新问题在这里添加代码Can';在C#脚本中找不到语法错误,c#,unity3d,compiler-errors,C#,Unity3d,Compiler Errors,我一直在用unity编写一个控制“单位”的C#脚本,简而言之,编译器告诉我('在我的整数函数中是意外的-下面粘贴的代码中的第20行-在'int cost'之前,我在任何地方都找不到我的脚本的问题。如果我删除整个函数,没有错误,并且它成功编译,因此我99%确定问题在于函数本身。对于缩进,我深表歉意,这是StackOverflow和bad的新问题在这里添加代码 using System.Collections; using System.Collections.Generic; using Unit
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
}
}
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
名称空间Iska.CNC.Units
{
公共类UnitHandler:MonoBehavior
{
公共静态UnitHandler实例;
[序列化字段]
私人BasicUnit Pitbull,RaiderBuggy;
私有void Start()
{
实例=此;
}
public(int-cost、int-attack、int-attackRange、int-health、int-tier)GetBasicUnitStats(字符串类型)
{
基本单位;
开关(类型)
{
案例“斗牛犬”:
单位=斗犬;
打破
“袭击者”案:
单位=RaiderBuggy;
打破
违约:
Log(“错误-找不到单元或单元不存在”);
返回(0,0,0,0,0);
}
返回(单位成本、单位攻击、单位攻击范围、单位生命值、单位等级);
}
公共void SetBasicUnitStats(转换类型)
{
foreach(在类型中转换子级)
{
foreach(子对象中的变换单元)
{
字符串unitName=child.name.Substring(0,child.name.Length-1).ToLower();
var stats=GetBasicUnitStats(单位名称);
Player.PlayerUnit pU;
if(type==Iska.CNC.Player.PlayerManager.instance.playerUnits)
{
pU=unit.GetComponent();
//在每个单位中设置单位统计信息
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)
{
//设置敌人的统计数据
}
//如果我们有任何升级或增益,现在添加它们
//增加单位统计数据的升级
}
}
}
}
}
您可以使用
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);
}
公共元组GetBasicUnitStats(字符串类型)
{
基本单位;
开关(类型)
{
案例“斗牛犬”:
单位=斗犬;
打破
“袭击者”案:
单位=RaiderBuggy;
打破
违约:
Log(“错误-找不到单元或单元不存在”);
返回Tuple.Create(0,0,0,0,0);
}
返回Tuple.Create(unit.cost、unit.attack、unit.attackRange、unit.health、unit.tier);
}
这要求您这样做。感谢@Sinatr解决了这个问题!