null reference exception problem with generic lists in unityscript (i think...)

Sorry if the following code is confusing!

I get a null reference exception on line noted below and can’t fix it…i’m fairly new to generic lists so I thought it might be because i was declaring a generic list at runtime but i get not better results when declaring outside of runtime… Hopefully there’s something glaringly obvious to someone with fresh eyes who can work their way through my mess of code…

this is part of a couple of large scripts which have been made to essentially create a coded sample playback system to recreate music in a slightly random way, this part of the code is to check if any of the major parts (that belong to the currently playing section) are actually playing (child_play) and if not to set one of them playing (chosen randomly). This is to avoid embarrassing total silences.

playing_section and current_section are both enum variables set through code and inspector (respectively)

(oh, the Debug.Log line just before the add line works as expected)

Any help will be greatly appreciated!

if (should_check_important_parts){
			var current_section_important_parts : List.<MusicMakerAnglesChild01>;// list of scripts
			var an_important_part_is_playing : boolean = false;
			for (var i : int = 0;i < important_parts.Count; i++){
				for (var ii : int = 0;ii < important_parts*.child_belongs_in.length; ii++){*

if (important_parts*.child_belongs_in[ii] ==
Debug.Log("current section important part to add: " +;
current_section_important_parts.Add(important_parts_);// NULL REFERENCE ERROR HERE**_
if (important_parts.child_play && !an_important_part_is_playing){
an_important_part_is_playing = true;
_ }

if (!an_important_part_is_playing){
var part_to_start : int = Random.Range(0,current_section_important_parts.Count);
current_section_important_parts[part_to_start].child_play = true;
_ }_
should_check_important_parts = false;
_ }*_


I think you need to go and learn how to use generic lists.

Like any other class type, List is a reference type, meaning both that it’s default value is null, and that it will get passed around by reference, rather than by value. (If you don’t know what that means, go learn some OOP)

This means your:

var list:List.<Type>;

will be null to begin with. You will have to create a new instance of List to do what you want:


On that note, the other things wrong with your code are: (not in any particular order)

  • Indentation (My god, use a tab or four spaces, not 2, 5, then 8!)
  • Inconsistante variable names (UseCamelCaseForVariables, not_this_unconventional_stuff)
  • Long/Bad variable names (MusicMakerAnglesChild01… need I say more?)
  • No comments or any empty lines to indicate any sort of code organization…
  • Inconsistant Spacing (Please, for the sake or readable code, put a space between a ) and a { !)

Fixing at least some of the above, as well as making them a habit, will not only make you a better coder, but it will also make coming back to the code and fixing any bugs much easier. At least when you come back to the code 4 months later you can at least still read the code and understand what it does, rather than having to throw it all out and start again :slight_smile: