Hello Community,
i have a little problem with my JS code.
var txtRadio : GUIText;
In the Update function:
txtRadio.text = (RadioName + ": " + CurrMusic.name);
This is just the little part of the whole Script, but i think it is enaught.
So what it does: It displays the name of the Radioname and the current Musikname on a GUIText.
This is the Error i get:
NullReferenceException: Object reference not set to an instance of an object
Radio.Update () (at Assets/Scripts/Radio.js:86)
Thanks if somone can help me, i tried some stuff out but the Error is still there.
That error means the component you are trying to access is invalid.
Can you show your entire script? How are you accessing currMusic?
Hey,
this is the entire script:
var Radion = 1;
var RadioName : String;
var _Radio_Gela_Express : AudioClip[];
var _RDS : AudioClip[];
var _RTL_102_5 : AudioClip[];
var _M2O : AudioClip[];
var _radioMargherita : AudioClip[];
var _KissKiss : AudioClip[];
private var CurrMusic : AudioClip;
private var R1CM : AudioClip;
private var R2CM : AudioClip;
private var R3CM : AudioClip;
private var R4CM : AudioClip;
private var R5CM : AudioClip;
private var R6CM : AudioClip;
private var R1 : AudioSource;
private var R2 : AudioSource;
private var R3 : AudioSource;
private var R4 : AudioSource;
private var R5 : AudioSource;
private var R6 : AudioSource;
private var R1N = "Radio Gela Express";
private var R2N = "RDS";
private var R3N = "RTL 102.5";
private var R4N = "M2O";
private var R5N = "Radio Margherita";
private var R6N = "Radio Kiss Kiss";
var txtRadio : GUIText;
function Start()
{
R1 = gameObject.AddComponent(AudioSource);
R2 = gameObject.AddComponent(AudioSource);
R3 = gameObject.AddComponent(AudioSource);
R4 = gameObject.AddComponent(AudioSource);
R5 = gameObject.AddComponent(AudioSource);
R6 = gameObject.AddComponent(AudioSource);
R1.loop = false;
R2.loop = false;
R3.loop = false;
R4.loop = false;
R5.loop = false;
R6.loop = false;
R1.dopplerLevel = 0;
R1.minDistance = 1;
R1.maxDistance = 3;
R2.dopplerLevel = 0;
R2.minDistance = 1;
R2.maxDistance = 3;
R3.dopplerLevel = 0;
R3.minDistance = 1;
R3.maxDistance = 3;
R4.dopplerLevel = 0;
R4.minDistance = 1;
R4.maxDistance = 3;
R5.dopplerLevel = 0;
R5.minDistance = 1;
R5.maxDistance = 3;
R6.dopplerLevel = 0;
R6.minDistance = 1;
R6.maxDistance = 3;
Radion = Random.Range(1,6);
}
function Update()
{
Swicht();
Radios();
Tracks();
txtRadio.text = (RadioName + ": " + CurrMusic.name);
}
function Radios()
{
if(Radion == 1)
{
R1.mute = false;
RadioName = R1N;
CurrMusic = R1.clip;
}
else
{
R1.mute = true;
}
if(Radion == 2)
{
R2.mute = false;
RadioName = R2N;
CurrMusic = R2.clip;
}
else
{
R2.mute = true;
}
if(Radion == 3)
{
R3.mute = false;
CurrMusic = R3.clip;
RadioName = R3N;
}
else
{
R3.mute = true;
}
if(Radion == 4)
{
R4.mute = false;
CurrMusic = R4.clip;
RadioName = R4N;
}
else
{
R4.mute = true;
}
if(Radion == 5)
{
R5.mute = false;
CurrMusic = R5.clip;
RadioName = R5N;
}
else
{
R5.mute = true;
}
if(Radion == 6)
{
R6.mute = false;
CurrMusic = R6.clip;
RadioName = R6N;
}
else
{
R6.mute = true;
}
}
function Tracks()
{
if(!R1.isPlaying)
{
R1.clip = _Radio_Gela_Express[Random.Range(0,_Radio_Gela_Express.length)];
R1.Play();
}
if(!R2.isPlaying)
{
R2.clip = _RDS[Random.Range(0,_RDS.length)];
R2.Play();
}
if(!R3.isPlaying)
{
R3.clip = _RTL_102_5[Random.Range(0,_RTL_102_5.length)];
R3.Play();
}
if(!R4.isPlaying)
{
R4.clip = _M2O[Random.Range(0,_M2O.length)];
R4.Play();
}
if(!R5.isPlaying)
{
R5.clip = _radioMargherita[Random.Range(0,_radioMargherita.length)];
R5.Play();
}
if(!R6.isPlaying)
{
R6.clip = _KissKiss[Random.Range(0,_KissKiss.length)];
R6.Play();
}
}
function Swicht()
{
Radion = Mathf.Clamp(Radion,1,6);
//Debug.Log("Swicht function in action");
if(Input.GetAxis("Mouse ScrollWheel") < 0 || Input.GetKeyUp(KeyCode.KeypadMinus))
{
Radion--;
}
if(Input.GetAxis("Mouse ScrollWheel") > 0 || Input.GetKeyUp(KeyCode.KeypadPlus))
{
Radion++;
}
}
The joke is, that the script is working perfect, the line where the Error is, is working perfect, but I can“t see that Error coming up on every start anymore.