Why is inserting variables into an array not working?
I am trying to insert track_easy, medium and hard into the "tracks" array, and I don't know why it isn't working. Help, please?
public MusicTrack[] tracks = new MusicTrack[3];
public int id;
public string name;
public MusicKit(string name, int id, MusicTrack track_Easy, MusicTrack track_Medium, MusicTrack track_Hard) {
this.id = id;
this.name = name;
// These are not inserted into the array.
tracks [0] = track_Easy;
tracks [1] = track_Medium;
tracks [2] = track_Hard;
}
}
First check your variables value. Are these null ?? And allocate memory to array "tracks" in Start method. like
public $$anonymous$$usic$$anonymous$$it(string name, int id, $$anonymous$$usicTrack track_Easy, $$anonymous$$usicTrack track_$$anonymous$$edium, $$anonymous$$usicTrack track_Hard) {
this.id = id;
this.name = name;
// These are not inserted into the array.
if(track_Easy != null)
tracks [0] = track_Easy;
if(track_$$anonymous$$edium != null)
tracks [1] = track_$$anonymous$$edium;
if(track_Hard != null)
tracks [2] = track_Hard;
}
void Start()
{
tracks = new $$anonymous$$usicTrack[3];
}
If this is a monobehaviour , you shouldn't have a constructor. If not, the Start function suggested by @saud_ahmed020 won't be called.
Anyways. What do you mean it's "not working"? It can mean a 100 things : code not compiling, getting a null ref error, soune not playing.
Try debugging by Debug.Loggigng out what you are actually putting into the array, and do the same when you try to read from the array and give your class instances id's so you know whether you write and read from the same array or are you e.g. creating duplicates without noticing
Your answer
![](https://koobas.hobune.stream/wayback/20220612043713im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
only keep every 10th entry in an array? 2 Answers
Array[] of children of a child 3 Answers
Getting the element that has the same element number on another list ? 2 Answers
Array 2D of data 1 Answer