- Home /
Question by
Grpm · Feb 19, 2014 at 09:15 PM ·
javascriptgetcomponent
Object reference not set to an instance of an object
Hello im new to scripting and im getting an error trying to acces an component on other script a float value in a array i can figure out what im doing wrong please help and if its other ways of doing this
first this is the script that is accesing the value
#pragma strict
var audioSource : GameObject;
var cancion : sonido = audioSource.GetComponent(sonido);
function Start (){
if (cancion.band[6] > 0.3){
Debug.Log ("mayor");
}
}
this is the second one that is getting accesed
var freqData:float[] = new float[8192];
var listener : AudioListener;
var band:float[];
var g:GameObject[];
function Start()
{
var n:int = freqData.length;
var k:int = 0;
for(var j=0;j<freqData.length;j++)
{
n /= 2;
if(!n) break;
k++;
}
band = new float[k+1];
g = new GameObject[k+1];
for (var i=0;i<band.length;i++)
{
band[i] = 0;
g[i] = new GameObject.CreatePrimitive(PrimitiveType.Sphere);
g[i].transform.position = Vector3(i,0,0);
}
InvokeRepeating("check", 0, 1.0/15.0); // update at 15 fps
}
function check()
{
listener.GetSpectrumData(freqData, 0, FFTWindow.Rectangular);
var k:int = 0;
var crossover:int = 2;
for(var i:int;i< freqData.length;i++)
{
var d = freqData[i];
var b = band[k];
band[k] = (d>b)? d:b; // find the max as the peak value in that frequency band.
if (i>crossover-3)
{
k++;
crossover *= 2; // frequency crossover point for each band.
g[k].transform.position.y = band[k]*32;
band[k] = 0;
}
}
}
private var sqrt = Mathf().Sqrt;
Comment
You should always check that an Object is not Null before you try and access it like this, especially if its another component on another GameObject.
function Start()
{
if (cancion != null)
{
if (cancion.band[6] > 0.3f)
{
Debug.Log("$$anonymous$$ayor");
}
}
else
{
Debug.LogError("Your cancion Object is NULL, you were about to access a NULL Object");
}
}
Start with that and see how you go. That should give you a clue i hope :)
Answer by Commander5518 · Feb 21, 2014 at 02:52 PM
This simply means, as mattyman174 said that one of your variables is unassigned or not assigned to something that Unity expects.