object reference not set to an instance of an object

141
06 марта 2018, 01:02

У меня есть 2 скрипта: Shop и things. В скрипте things у меня есть класс thing, и объект класса thing thingsS[]. В скрипте Shop я прогоняя через цикл пытаюсь сравнить

 for (int i =0;i<=1000;i++)
    {
        if (Things.thingsS[i].prisutstvie == false)//ругается на эту строчку
        {
            Debug.Log("click");
            Things.thingsS[i].prisutstvie = true;
            Things.thingsS[i].rare = rare;
            Things.thingsS[i].type = type;
            for(int j = 0; j<=3;j++)
            {
                if(sumstat[j] == 1)
                {
                    force = force * mno[j];
                    Things.thingsS[i].force = force;
                }
                if(sumstat[j] == 2)
                {
                    lovk = lovk * mno[j];
                    Things.thingsS[i].lovk = lovk;
                }
                if(sumstat[j]== 3)
                {
                    luck = luck * mno[j];
                    Things.thingsS[i].luck = luck;
                }                  
            }
            break;
        }
    }

Вот сам класс

public class Things : MonoBehaviour {
public static int mas = 1000;
public static thing[] thingsS = new thing[mas];
public class  thing
{
    public int  force =0;
    public int lovk = 0;
    public float luck = 0;
    public int rare = 4;
    public int  sum = 0; 
    public int type = 1;
    public bool prisutstvie = false;
}

Как решить данную проблему?

Answer 1

Вам нужно создать объект класса (что бы сработал конструктор), а потом к нему обращаться

Things someThings = new Things();
        for (int i = 0; i <= 1000; i++)
        {
            if (someThings.thingsS[i].prisutstvie == false)//ругается на эту строчку
            {
                Debug.Log("click");
                someThings.thingsS[i].prisutstvie = true;
                someThings.thingsS[i].rare = rare;
                someThings.thingsS[i].type = type;
                for (int j = 0; j <= 3; j++)
                {
                    if (sumstat[j] == 1)
                    {
                        force = force * mno[j];
                        someThings.thingsS[i].force = force;
                    }
                    if (sumstat[j] == 2)
                    {
                        lovk = lovk * mno[j];
                        someThings.thingsS[i].lovk = lovk;
                    }
                    if (sumstat[j] == 3)
                    {
                        luck = luck * mno[j];
                        someThings.thingsS[i].luck = luck;
                    }
                }
                break;
            }
READ ALSO
Проблема локализации Google карты

Проблема локализации Google карты

Доброго времени суток, столкнулся с такой проблемой, при попытке получить локализированные русские данные Google Maps возвращает результаты...

229