- Home /
[SOLVED] Any List I make in this script becomes null, but it works fine with other scripts...
This is my first time asking a question here and it might look pretty silly after the fact, but I just can't figure out why my lists are null here:
I need a list that I can fill in with objects of a certain class from the editor. I've also tried changing the list to public/using get&set/instantiating the new list from start(), but nothing seems to work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Ink.Runtime;
public class DialogueManager : MonoBehaviour
{
[SerializeField] private List<DialogueBox> dialogueBoxes = new List<DialogueBox>();
[SerializeField] private List<InteractableData> speakers = new List<InteractableData>();
void Start()
{
if (speakers != null)
{
Debug.Log("List of Interactable Data is null.");
}
if (dialogueBoxes != null)
{
Debug.Log("List of Dialogue Boxes is null.");
}
//Both checks return null,
}
Meanwhile, I have another script where I do the same thing, even filling it with objects of a certain class through the editor and it works:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class InteractionUIHandler : MonoBehaviour
{
[SerializeField] private List<InteractableName> interactableNames = new List<InteractableName>();
}
Is there something I'm doing wrong or different here? There are no compiler errors too, And I've checked and rechecked that I populated the lists in the editor and there doesn't seem to be anything wrong with the code for InteractableData and DialogueBox either...
Do you know that your lists are null because of your debug logs or are you getting null reference errors when trying to access them? Because if its just the debug.logs then the issue is that you've mixed up '!=' and '==' in your if statements.
Hi! Thanks for the reply. I know that it is null because I got null reference exceptions when I tried to access the list. Which I pinpointed to happen when I used foreach loops. A bunch of debug logs later and the list itself being null is where I got. I did check with an == null when using a long list of other debug checks to find where the error was. But that was before accessing the list. I can try it again at the start() in case there's a difference
[UPDATE: Okay, me being kinda dumb and my checks were wrong, but after fixing that, I was able to find the real null reference error which happened to be just a mix-up in the order that made sure one variable was assigned a value before referencing it!]
Your answer
Follow this Question
Related Questions
subfolders missing from standard assets folder (Unity 3) 3 Answers
Unity project weird behaviour - Scenes not opening / script links not working 1 Answer
MissingMethodException: Method not found- Error on fps script 1 Answer
Missing textures after import package 0 Answers
my hierarchy is gone 1 Answer