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.
