来自另一个类的Unity调用方法

时间:2019-06-03 17:22:11

标签: unity3d nullpointerexception singleton

当我尝试从另一个脚本更改纹理颜色时,出现了空指针异常。

NullReferenceException: Object reference not set to an instance of an object

在同一文件中有效。

SwapColor(myTex, Color.black, Color.green,1f);

给出空指针异常

//PartsColorCustomizer.Instance.SwapColor(myTex, Color.black, Color.green, 1f);

这是我试图在另一个称为PartsColorCustomizer的类中调用的方法

public void SwapColor(Texture2D tex, Color original, Color swap, float threshold)
{
    for (int x = 0; x < tex.width; x++)
    {
        for (int y = 0; y < tex.height; y++)
        {
            Color col = tex.GetPixel(x, y);
            bool r = Mathf.Abs(original.r - col.r) <= threshold;
            bool g = Mathf.Abs(original.g - col.g) <= threshold;
            bool b = Mathf.Abs(original.b - col.b) <= threshold;
            bool a = Mathf.Abs(original.a - col.a) <= threshold;
            if (r && g && b && a)
            {

                //tex.SetPixel(x, y, swap);
            }
            if (col == Color.white) {
                tex.SetPixel(x, y, swap);
            }
            if (col == Color.magenta)
            {
                tex.SetPixel(x, y, swap);
            }
        }
    }
    tex.Apply();
}

我设置的实例

 void Awake()
{
    if (_Instance != null)
    {
        _Instance = this;
        //LoadJson();
    }
}

0 个答案:

没有答案