gpt4 book ai didi

c# - Unity3D C# - 变量范围

转载 作者:行者123 更新时间:2023-12-02 04:39:01 34 4
gpt4 key购买 nike

我正在将我的 Unity3D 游戏从 JS 转换为 C#,我遇到了这个函数的一些问题:

void ReorientationNaming(GameObject g)
{
///find the cube and its bounds
GameObject tobename = g;
Bounds cubebound = tobename.renderer.bounds;
string namex;
string namey;
string namez;

GameObject[] allAxisList = GameObject.FindGameObjectsWithTag("AxisPlain");
foreach(GameObject allAxis in allAxisList)
{
Bounds axisbound = allAxis.renderer.bounds;
if (cubebound.Intersects(axisbound))
{
if (allAxis.name.Contains("x"))
{
namex = allAxis.name;
namex = namex.Substring(1,1);
//print("namex" + namex);
}
if (allAxis.name.Contains("y"))
{
namey = allAxis.name;
namey = namey.Substring(1,1);
}
if (allAxis.name.Contains("z"))
{
namez = allAxis.name;
namez = namez.Substring(1,1);
}
}
tobename.name = namex+namey+namez;//<-- this line is the problem!
}
}

最后一行给我错误:

Assets/Cumetry/MainGameLogic.cs(136,41): error CS0165: Use of unassigned local variable `namex'
Assets/Cumetry/MainGameLogic.cs(136,41): error CS0165: Use of unassigned local variable `namey'
Assets/Cumetry/MainGameLogic.cs(136,41): error CS0165: Use of unassigned local variable `namez'

我相信这是我声明字符串的方式。知道我该如何解决这个问题吗?

最佳答案

改变

    string namex;
string namey;
string namez;

    string namex = string.Empty;
string namey = string.Empty;
string namez = string.Empty;

关于c# - Unity3D C# - 变量范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21306181/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com