Getting this big error but i could not find why it is coming
I have attached this script to jet menu controller gameobject and left and right button are assigned proper funtions from jet menu controller gameobject select btn is also assigned proper function from missile select script which is attached to jet menu controller gameobject.
Left and right arrow are working but when clicked give this error. How can i solve this problem?
error of line 70
NullReferenceException: Object reference not set to an instance of an object
MissileSelect.CheckIfMissileIsUnlock () (at Assets/Scripts/MissileSelect.cs:70)
MissileSelect.NextMissile () (at Assets/Scripts/MissileSelect.cs:52)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
error of line 115
NullReferenceException: Object reference not set to an instance of an object
MissileSelect.SelectMissile () (at Assets/Scripts/MissileSelect.cs:115)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
And the code is here ,error at line 70 and 115
public GameObject[] availableMissiles;
private int currentMissileIndex;
public Text selectedText;
public GameObject CoinIcon;
public Image selectBtn_Image;
public Sprite button_Green, button_Blue;
private int missilePrice;
private bool[] missiles;
public Text coinScoreText;
// Start is called before the first frame update
private string missile1 = "0", missile2 = "2000", missile3 = "5000", missile4 = "8000";
void Start()
{
InitializeCharacters();
}
// Update is called once per frame
void Update()
{
}
void InitializeCharacters()
{
currentMissileIndex = GameManager.instance.missileSelected_Index;
for (int i = 0; i < availableMissiles.Length; i++)
{
availableMissiles[i].SetActive(false);
}
availableMissiles[currentMissileIndex].SetActive(true);
missiles = GameManager.instance.missiles;
}
public void NextMissile()
{
availableMissiles[currentMissileIndex].SetActive(false);
if (currentMissileIndex + 1 == availableMissiles.Length)
{
currentMissileIndex = 0;
}
else
{
currentMissileIndex++;
}
availableMissiles[currentMissileIndex].SetActive(true);
CheckIfMissileIsUnlock();
}
public void PreviousMissile()
{
availableMissiles[currentMissileIndex].SetActive(false);
if (currentMissileIndex - 1 == -1)
{
currentMissileIndex = availableMissiles.Length - 1;
}
else
{
currentMissileIndex--;
}
availableMissiles[currentMissileIndex].SetActive(true);
CheckIfMissileIsUnlock();
}
void CheckIfMissileIsUnlock()
{
if (missiles[currentMissileIndex] == true) // error at this line
{
//if the jet is unlocked
CoinIcon.SetActive(false);
if (currentMissileIndex == GameManager.instance.missileSelected_Index)
{
selectBtn_Image.sprite = button_Green;
selectedText.text = "Selected";
}
else
{
selectBtn_Image.sprite = button_Blue;
selectedText.text = "Select?";
}
}
else
{
//if the jet is locked
selectBtn_Image.sprite = button_Blue;
CoinIcon.SetActive(true);
if (currentMissileIndex == 0)
{
selectedText.text = missile1;
missilePrice = 0;
}//change price of jet here
if (currentMissileIndex == 1)
{
selectedText.text = missile2;
missilePrice = 2000;
}
if (currentMissileIndex == 2)
{
selectedText.text = missile3;
missilePrice = 5000;
}
if (currentMissileIndex == 3)
{
selectedText.text = missile4;
missilePrice = 8000;
}
}
}
public void SelectMissile()
{
if (!missiles[currentMissileIndex])// error at this line
{
//if the jet is not unlocked- meaninig unlock it
if (currentMissileIndex != GameManager.instance.missileSelected_Index)
{
//unlock jet if u have enough coins
if (GameManager.instance.coinScore >= missilePrice)//change price of jet here
{
GameManager.instance.coinScore -= missilePrice;//change price of jet here
selectBtn_Image.sprite = button_Green;
selectedText.text = "Selected";
CoinIcon.SetActive(false);
missiles[currentMissileIndex] = true;
coinScoreText.text = GameManager.instance.coinScore.ToString();
GameManager.instance.missileSelected_Index = currentMissileIndex;
GameManager.instance.missiles = missiles;
GameManager.instance.SaveGameData();
}
else
{
print("not enough coins");
}
}
}
else
{
selectBtn_Image.sprite = button_Green;
selectedText.text = "Selected";
GameManager.instance.missileSelected_Index = currentMissileIndex;
GameManager.instance.SaveGameData();
}
}
}
currentMissileIndex is int type and it is assigned value from gameManager script
missiles is bool type array.
Please help me to solve this problem.