Search Unity

[JS] Object reference not set to an instance of an object

Discussion in 'Scripting' started by K0enich, Jul 24, 2014.

  1. K0enich

    K0enich

    Joined:
    Apr 14, 2014
    Posts:
    4
    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.
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    That error means the component you are trying to access is invalid.

    Can you show your entire script? How are you accessing currMusic?
     
  3. K0enich

    K0enich

    Joined:
    Apr 14, 2014
    Posts:
    4
    Hey,

    this is the entire script:

    Code (JavaScript):
    1. var Radion = 1;
    2. var RadioName : String;
    3.  
    4. var _Radio_Gela_Express : AudioClip[];
    5. var _RDS : AudioClip[];
    6. var _RTL_102_5 : AudioClip[];
    7. var _M2O : AudioClip[];
    8. var _radioMargherita : AudioClip[];
    9. var _KissKiss : AudioClip[];
    10.  
    11. private var CurrMusic : AudioClip;
    12.  
    13. private var R1CM : AudioClip;
    14. private var R2CM : AudioClip;
    15. private var R3CM : AudioClip;
    16. private var R4CM : AudioClip;
    17. private var R5CM : AudioClip;
    18. private var R6CM : AudioClip;
    19.  
    20. private var R1 : AudioSource;
    21. private var R2 : AudioSource;
    22. private var R3 : AudioSource;
    23. private var R4 : AudioSource;
    24. private var R5 : AudioSource;
    25. private var R6 : AudioSource;
    26.  
    27. private var R1N = "Radio Gela Express";
    28. private var R2N = "RDS";
    29. private var R3N = "RTL 102.5";
    30. private var R4N = "M2O";
    31. private var R5N = "Radio Margherita";
    32. private var R6N = "Radio Kiss Kiss";
    33.  
    34. var txtRadio : GUIText;
    35.  
    36. function Start()
    37. {
    38.     R1 = gameObject.AddComponent(AudioSource);
    39.     R2 = gameObject.AddComponent(AudioSource);
    40.     R3 = gameObject.AddComponent(AudioSource);
    41.     R4 = gameObject.AddComponent(AudioSource);
    42.     R5 = gameObject.AddComponent(AudioSource);
    43.     R6 = gameObject.AddComponent(AudioSource);
    44.  
    45.     R1.loop = false;
    46.     R2.loop = false;
    47.     R3.loop = false;
    48.     R4.loop = false;
    49.     R5.loop = false;
    50.     R6.loop = false;
    51.  
    52.     R1.dopplerLevel = 0;
    53.     R1.minDistance = 1;
    54.     R1.maxDistance = 3;
    55.  
    56.     R2.dopplerLevel = 0;
    57.     R2.minDistance = 1;
    58.     R2.maxDistance = 3;
    59.  
    60.     R3.dopplerLevel = 0;
    61.     R3.minDistance = 1;
    62.     R3.maxDistance = 3;
    63.  
    64.     R4.dopplerLevel = 0;
    65.     R4.minDistance = 1;
    66.     R4.maxDistance = 3;
    67.  
    68.     R5.dopplerLevel = 0;
    69.     R5.minDistance = 1;
    70.     R5.maxDistance = 3;
    71.  
    72.     R6.dopplerLevel = 0;
    73.     R6.minDistance = 1;
    74.     R6.maxDistance = 3;
    75.  
    76.     Radion = Random.Range(1,6);
    77. }
    78.  
    79. function Update()
    80. {
    81.     Swicht();
    82.     Radios();
    83.     Tracks();
    84.     txtRadio.text = (RadioName + ": " + CurrMusic.name);
    85. }
    86.  
    87. function Radios()
    88. {
    89.     if(Radion == 1)
    90.     {
    91.         R1.mute = false;
    92.         RadioName = R1N;
    93.         CurrMusic = R1.clip;
    94.     }
    95.     else
    96.     {
    97.         R1.mute = true;
    98.     }
    99.  
    100.     if(Radion == 2)
    101.     {
    102.         R2.mute = false;
    103.         RadioName = R2N;
    104.         CurrMusic = R2.clip;
    105.     }
    106.     else
    107.     {
    108.         R2.mute = true;
    109.     }
    110.  
    111.     if(Radion == 3)
    112.     {
    113.         R3.mute = false;
    114.         CurrMusic = R3.clip;
    115.         RadioName = R3N;
    116.     }
    117.     else
    118.     {
    119.         R3.mute = true;
    120.     }
    121.  
    122.     if(Radion == 4)
    123.     {
    124.         R4.mute = false;
    125.         CurrMusic = R4.clip;
    126.         RadioName = R4N;
    127.     }
    128.     else
    129.     {
    130.         R4.mute = true;
    131.     }
    132.  
    133.     if(Radion == 5)
    134.     {
    135.         R5.mute = false;
    136.         CurrMusic = R5.clip;
    137.         RadioName = R5N;
    138.     }
    139.     else
    140.     {
    141.         R5.mute = true;
    142.     }
    143.  
    144.         if(Radion == 6)
    145.     {
    146.     R6.mute = false;
    147.     CurrMusic = R6.clip;
    148.     RadioName = R6N;
    149.     }  
    150.     else
    151.     {
    152.         R6.mute = true;
    153.     }
    154. }
    155.  
    156. function Tracks()
    157. {
    158.     if(!R1.isPlaying)
    159.     {
    160.         R1.clip = _Radio_Gela_Express[Random.Range(0,_Radio_Gela_Express.length)];
    161.         R1.Play();
    162.     }
    163.     if(!R2.isPlaying)
    164.     {
    165.         R2.clip = _RDS[Random.Range(0,_RDS.length)];
    166.         R2.Play();
    167.     }
    168.     if(!R3.isPlaying)
    169.     {
    170.         R3.clip = _RTL_102_5[Random.Range(0,_RTL_102_5.length)];
    171.         R3.Play();
    172.     }
    173.     if(!R4.isPlaying)
    174.     {
    175.         R4.clip = _M2O[Random.Range(0,_M2O.length)];
    176.         R4.Play();
    177.     }
    178.     if(!R5.isPlaying)
    179.     {
    180.         R5.clip = _radioMargherita[Random.Range(0,_radioMargherita.length)];
    181.         R5.Play();
    182.     }
    183.     if(!R6.isPlaying)
    184.     {
    185.         R6.clip = _KissKiss[Random.Range(0,_KissKiss.length)];
    186.         R6.Play();
    187.     }
    188. }
    189.  
    190. function Swicht()
    191. {
    192.     Radion = Mathf.Clamp(Radion,1,6);
    193.     //Debug.Log("Swicht function in action");
    194.     if(Input.GetAxis("Mouse ScrollWheel") < 0 || Input.GetKeyUp(KeyCode.KeypadMinus))
    195.     {
    196.         Radion--;
    197.     }
    198.     if(Input.GetAxis("Mouse ScrollWheel") > 0 || Input.GetKeyUp(KeyCode.KeypadPlus))
    199.     {
    200.         Radion++;
    201.     }
    202. }
    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.
     
    Last edited: Jul 24, 2014