I have a problem with my code in Unity and can't decide it by myself. Here is my code
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
[Serializable]
public class PlayerVoice
{
public int voiceSnap;
public string voiceValue;
}
public class GetVoice : MonoBehaviour
{
public static GetVoice instance;
public PlayerVoice[] playerVoice;
public int[] snaps;
public string[] voiceChangeValue;
public void Awake()
{
instance = this;
}
public void LoadVoice()
{
var count = MainInfo.instatnce.mainCount;
playerVoice = new PlayerVoice[count];
for (int i = 0; i < count; i++)
{
playerVoice[i].voiceSnap = snaps[i];
playerVoice[i].voiceValue = voiceChangeValue[i];
}
}
}
But in starting scene I get error in this line
playerVoice[i].voiceSnap = snaps[I];
this is error text from console
NullReferenceException: Object reference not set to an instance of an object GetVoice+d__43.MoveNext () (at Assets/Script/GetVoice.cs:36)
if I change playerVoice Length into inspector, the problem is resolved. I can't understand why I catch this error. Help please