null reference with array if I don't have object selected

I am having a very odd problem.

if (orginhit.point.y <= 0.01 || MTL.townPixelArray[orgin.y * MTL.townPixelWidth + orgin.x].f > 0){If I try to access MTL.townPixelArray (it is correctly sized) the game will throw that error. But this is only when I do not have the game object the script’s attached to selected in the hierarchy.

If I have the game object selected in the hierarchy the script runs fine. If I do not have it selected it will error.
So if I have a_GameInit selected it runs fine.
if I have a_MainCamera selected it will error.


TownLayout is ran once by a central controller script at startup.
yield GetComponent(TownLayout).initTownLayout();Here is the code (ran once) that resizes the array.

    //TownPixel   is a class
    MTL.terrainPixelSize = GetComponent(TerrainGenerator).MainTerrain.TerrainPixelSize;
    var f : float = GetComponent(TerrainGenerator).MainTerrain.PerlinTex.width * MTL.terrainPixelSize;
    f = f / MTL.rayDistance;
    MTL.townPixelArray  =  new TownPixel[ f * f + f];

What am I doing wrong - how can I fix this? Any extra information needed just ask.

How do you assign/instantiate a value to MTL?

var MTL : TownLayoutBase; 
function initTownLayout(){
    //first resize townPixelArray to correct size
    //than for every town, try to find correct place that isn't taken .  Yield at finish of new town
        //if not taken than check height around it.  if average is good continue
        
//GetComponent(TerrainGenerator).MainTerrain.PerlinTex.width * 
    MTL.terrainPixelSize = GetComponent(TerrainGenerator).MainTerrain.TerrainPixelSize;
    var f : float = GetComponent(TerrainGenerator).MainTerrain.PerlinTex.width * MTL.terrainPixelSize;
    f = f / MTL.rayDistance;
    MTL.townPixelWidth = f;
    MTL.townPixelArray  =  new TownPixel[ f * f + f];
    yield;
    Debug.Log("f=" + f);
    for (towncount = 0; towncount <= MTL.townsGenerate ; towncount += 1){
        //For every town
        //Only update towncount *********  if town has been placed

        Debug.Log("Attempting to create town orgin");
        if (TownLayoutOrgin()){
            towncount += 1;
        }
        
    }

}

MTL is a class.

class TownLayoutBase{

    var rayDistance : float = 5.0 ; //How far each ray goes.    Perlinres * pixelsize / rayDistance   is new image size
    var townsGenerate : int = 5; //turn this into array class that holds invidual town data.
        //name (generate?)
        //size
        //type
        //ectect
    var townOrgin : TownOrgin;
    var townCheckSize : int = 50; //how many check-loops are allowed
    var townBorderSize : int = 15; //How far from the border we start at
    var townPixelArray : TownPixel[] = new TownPixel[1];
    var townPixelWidth : int ;//= 0; //width of the array
     var terrainPixelSize : float = 0.0;
}
 class TownPixel{
    //Used as a 2d array.  Contains power, distance, and count
    var f : float = 0.0;
    var d : float = 0.0;
    var c : float = 0.0;
}

If I have a_GameInit selected in the editor the script runs fine. If I do not it throws an error.

This more basic version will also throw an error if I do not have a_GameInit selected.

MTL.townPixelArray  =  new TownPixel[55];
MTL.townPixelArray[0].f = 5.0;

I’m still not picking up where you assign MTL a value. Something like:

MTL = new TownLayoutBase();

Erp, that got cut off when pasting the code.

Can you show it then?

I will make a blind guess, perhaps when you select an object of the scene it is updated first (before everything else).

Why not try to instantiate MTL in the awake function, instead on your own constructor.

private var target : GameObject;

function Awake () {
target = GameObject.FindWithTag ("Player");
}

Does this happen to Javascript also?