如何使用其他脚本中的变量作为方法参数

问题描述 投票:0回答:1

我一直在努力寻找一种需要检查的方法,然后更改来自单独脚本和对象的变量。我正在尝试检查数组以查找不为null的条目以及占位符中尚未使用的条目。当这两个都成立时,应该将该数组的内容复制到我的ActualReadIn字符串变量中,该变量保存在另一个脚本中。 类别是我正在读取的字符串数组。

这是我的代码:

int checkForNotNull(string[] a)
    {
        for (int x = 0; x < a.Length; x++)
        {
            if (a[x]!= "" && orbScript1.actualReadIn != a[x] && orbScript2.actualReadIn != a[x] && orbScript3.actualReadIn != a[x] && orbScript4.actualReadIn != a[x]
                && orbScript5.actualReadIn != a[x] && orbScript6.actualReadIn != a[x]&& orbScript7.actualReadIn != a[x] && orbScript8.actualReadIn != a[x])
                //^^ This should both check to see if the entry is not nothing, and see if the entry matches any others orbs.

                return x;
        }
        return -1;
    }

^^此代码检查以确保其他占位符尚未包含数组条目


void readArray(string actualRead)
    {
        if (taskPhase == 1)
        {
            if (actualRead == "")
            {
                actualRead = Categories[checkForNotNull(Categories)];
            }
        }

        if (taskPhase == 2)
        {

        }
    }

^^此脚本上的参数应为orbScript1.actualReadIn。这是我要填充的字符串变量。当我尝试调用方法时,问题就来了

readArray(orbScript1.actualReadIn);

没有任何错误,但是没有填充orbScript1上的值。我对此很陌生,所以如果我做的事情形式不好或不常见,请告诉我。

如果您需要澄清,我会尽力提供。

谢谢。

c# unity3d
1个回答
0
投票

我想我明白了。事实证明,readArray()完全没有用。我实际获得输出的方式是

if (orbScript1.actualReadIn == "")
{
orbScript1.actualReadIn = Categories[checkForNotNull (Categories)];
}

此方法不要求提供脚本外的参数,而是填充对象上的actualReadIn变量。

© www.soinside.com 2019 - 2024. All rights reserved.