List resets on enter playmode

Hi. So my problem is that when I go into playmode, my serialization made in inspector for some reason resets and i get null exception and so my code don’t work.

Some of my code:

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

public class TrackList : MonoBehaviour
{
    [SerializeField] private List<Track> trackObjects;
    [SerializeField] private List<AudioClip> audioTracks;
    public AudioClip CurrentTrack { get; private set; }
    public static bool TrackChanged { get; set; }

    private void Awake()
    {
        if (trackObjects == null)
        {
            trackObjects = new List<Track>();
        }
    }

    public void PrepareTracklistButtons(Game game, Clicker clicker)
    {
        for (int i = 0; i <= trackObjects.Count - 1 ; i++)   //just i < ...Count, try it
        {
            GameObject trackObject = trackObjects[i].gameObject;
            Button button = trackObject.GetComponentInChildren<Button>();

            button.onClick.AddListener(game.SwitchCanvas);
            button.onClick.AddListener(() => ChangeData(clicker, trackObject)); //change elements in playmode
        }
    }

Here i get an exception in line

GameObject trackObject = trackObjects[i].gameObject;

Cause trackObjects elements are becoming null for some reason

This is TrackList object before I enter playmode:

as you can see trackObjects are serialized, but after I enter playmode it gets reset to null
Track script is attached to Track object, of course

I also don’t have any code reseting it somewhere in start or awake methods

This is my track class:

using UnityEngine;

public class Track : MonoBehaviour
{
    private void Start()
    {
        
    }

    private void Update()
    {
        
    }
    //private bool completed;
    //private AudioClip clip;
}

Its empty, could this be a reason? I tried to make fields public, still no use

What I am trying to do is to get reference from GO to which track is attached to, I used to have list of GameObjects and it worked fine, I can get back to this implementation of course, but I wish to know, why current implementation doesnt work,
Thanks in advance.

Of course, the code here makes a new list …

it was added later, if i remove this code, It doesnt change anything
plus it creates a new list ONLY if it is null :slight_smile:

Are these referenced components on objects in the same scene, and the objects and the components are not being destroyed when entering the scene?

I also don’t see any reason why this shouldn’t work, it’s a common pattern. CodeSmile is on a good track: are these just regular GOs in the same scene, or is there some sort of trickery?
More info on the setup always helps.

Which btw is unnecessary, you can simply new() the field so it’ll never be null:

[SerializeField] private List<Track> trackObjects = new();

Won’t change any existing scripts on objects since they may already have the field serialized as null. But it’s good habit to get into to initialize fields with a default value.

that’s right, everything’s on the same scene, I need here 2 'scenes’which I implemented with 2 canvases, i just switch them in hierarchy when needed

I dunno, guys, it’s definitely some bug, I made some experimenting around:
firstly I tried to create a new list a do the same thing with it, it never reseted, then
I thought maybe the problem is with name of class or variable name , replaced it and it broke in new class, then went back to old class and… uuhhmm so what I’m trying to say is that I have now all the same code with the same hierarchy and everything works fine for some reason, I dont know wtf was that xD
Thanks anyway for your replies