0

I received this error I'm using unity 2020.3.25f1. (see the picture to know the line 45.)

enter image description here

Please help to fix this issue.

Thanks

NullReferenceException: Object reference not set to an instance of an object BBG.PopupManager.Awake () (at Assets/Sudoku/Framework/Scripts/Popup/PopupManager.cs:45)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BBG
{
public class PopupManager : SingletonComponent<PopupManager>
{
    #region Classes

    [System.Serializable]
    private class PopupInfo
    {
        [Tooltip("The popups id, used to show the popup. Should be unique between all other popups.")]
        public string popupId = "";

        [Tooltip("The Popup component to show.")]
        public Popup popup = null;
    }

    #endregion

    #region Inspector Variables

    [SerializeField] private List<PopupInfo> popupInfos = null;

    #endregion

    #region Member Variables

    private List<Popup> activePopups;

    #endregion

    #region Unity Methods

    protected override void Awake()
    {
        base.Awake();

        activePopups = new List<Popup>();

        for (int i = 0; i < popupInfos.Count; i++)
        {
            popupInfos[i].popup.Initialize();
        }
    }

    #endregion

    #region Public Methods

    public void Show(string id)
    {
        Show(id, null, null);
    }

    public void Show(string id, object[] inData)
    {
        Show(id, inData, null);
    }

    public void Show(string id, object[] inData, Popup.PopupClosed popupClosed)
    {
        Popup popup = GetPopupById(id);

        if (popup != null)
        {
            if (popup.Show(inData, popupClosed))
            {
                activePopups.Add(popup);
            }
        }
        else
        {
            Debug.LogErrorFormat("[PopupController] Popup with id {0} does not exist", id);
        }
    }

    public bool CloseActivePopup()
    {
        if (activePopups.Count > 0)
        {
            int index = activePopups.Count - 1;

            Popup popup = activePopups[index];

            if (popup.CanAndroidBackClosePopup)
            {
                popup.Hide(true);
            }

            return true;
        }

        return false;
    }

    public void OnPopupHiding(Popup popup)
    {
        for (int i = activePopups.Count - 1; i >= 0; i--)
        {
            if (popup == activePopups[i])
            {
                activePopups.RemoveAt(i);

                break;
            }
        }
    }

    #endregion

    #region Private Methods

    private Popup GetPopupById(string id)
    {
        for (int i = 0; i < popupInfos.Count; i++)
        {
            if (id == popupInfos[i].popupId)
            {
                return popupInfos[i].popup;
            }
        }

        return null;
    }

    #endregion
}

}

Rachid
  • 1
  • 2

1 Answers1

1

your bug is here

 [SerializeField] private List<PopupInfo> popupInfos = null;

you can try this, but it will not give you anything, except it will hide an exception, but you have to get popupInfos list from somewhere

 [SerializeField] private List<PopupInfo> popupInfos = new List<PopupInfo>();
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Since `PopupInfo` is a `[Serializable]` type then this shouldn't change anything since the Unity Serializer would automatically initialize the list since it is a serialized field. – derHugo Mar 11 '22 at 21:51
  • @derHugo Maybe you are right, maybe OP marked [Serializable] just in case. I am not an expert in Unity3d. For me it is just a common sense. I thought it is a private field but I couldn't see any init code in the scope and debuger show exception only for this line – Serge Mar 11 '22 at 21:57