首页 > 解决方案 > 如何使用来自不同脚本的变量作为方法参数

问题描述

我一直在努力使用需要检查的方法,然后更改来自单独脚本和对象的变量。我正在尝试检查数组中是否存在非空条目以及占位符中尚未使用的条目。当这两个都为真时,它应该将该数组的内容复制到我的 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

解决方案


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

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

此方法不要求脚本之外的参数,并填充对象上的 actualReadIn 变量。


推荐阅读