Search Unity

NullReferenceException: / Question.

Discussion in 'Scripting' started by larswik, Oct 6, 2015.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I have some code here that plays 1 of 3 audio sounds for a character randomly. I am getting an error message
    On row 8 I created an empty AudioSource object called growl which is why I assume I am getting this error message on line 14. It is checking to see if the object is playing which is null because it has not been assigned yet.

    I guess my question is how can I setup / instantiate an empty AudioSource that I can assign later in C#? In objective C I should do NSString *string;, or NSString *string = null;

    Code (CSharp):
    1. public class zombieMove : MonoBehaviour {
    2.  
    3.     public float speed = 1;
    4.     public AudioSource growl1;
    5.     public AudioSource growl2;
    6.     public AudioSource growl3;
    7.     bool audioPlaying = false;
    8.     AudioSource growl;
    9.    
    10.     void Update () {
    11.         Vector3 sPoint = new Vector3(0f,0f,speed);
    12.         transform.Translate(sPoint);
    13.  
    14.         if(!growl.isPlaying){
    15.             audioPlaying = false;
    16.         }
    17.  
    18.         if(Random.value < 0.005 && audioPlaying == false){
    19.  
    20.             int rand = Random.Range(0,3); // Range states that the max num is excluded, so int is 1 higher then needed.
    21.             Debug.Log(rand);
    22.  
    23.             if(rand == 0){
    24.                 growl = growl1;
    25.             }
    26.             if(rand == 1){
    27.                 growl = growl2;
    28.             }
    29.             if(rand == 2){
    30.                 growl = growl3;
    31.             }
    32.  
    33.             if(growl.isPlaying){
    34.                 audioPlaying = true;
    35.                 Debug.Log("playing");
    36.             }
    37.             else{
    38.                 growl.volume = .3f;
    39.                 growl.Play();
    40.                 Debug.Log("Voice " + rand);
    41.             }
    42.         }
    43.     }
    44. }
    45.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    First things first.

    Look up "arrays"...

    ...

    ok, it would be far better to have a single AudioSource and an Array of AudioClips. One jukebox, many tracks. not lots of Jukeboxes with one track each. It'll also really simplify your code for what you are trying to do.


    You never assign anything to growl. Assuming you have an audiosource attached to the same gameobject as this script you will at the very least need a Start/Awake function and a GetComponent<AudioSource>() (another reason for having a single AudioSource... )
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I get what you are saying. If I create an array I could have the AudioClips stored in the array and the use a random number to select the AudioClip and assign it to the AudioSource. I'm guessing that arrays can hold objects and not just primitive data types? I will test out your method and see if I can reduce down the code. Thanks.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    A bit of sample code to get you started:
    Code (csharp):
    1. public AudioClip[] growls;
    2. public AudioSource growlSource;
    3. void Update() {
    4. . . . .
    5. AudioClip chosenGrowl = growls[Random.Range(0, growls.Length)];
    6. growlSource.PlayOneShot(chosenGrowl);
    7. }
    Not only is this really compact, it also lets you throw in as many or as few growl effects later without changing a line of code.

    - - -

    Components are special in that you need Unity's help to create them, largely because they have to be attached to a GameObject. To create a new one from scratch in code would look something like:
    Code (csharp):
    1. GameObject newGO = new GameObject("The new guy");
    2. growlSource = newGO.AddComponent<AudioSource>();
    From there you can use growlSource to add a clip, adjust the volume, etc as normal.
     
  5. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    That sample source is gorgeous and less wordy. Line 1 is interesting and will test it out, looks like you are making the growls array public so in unity I'm guessing I would have endless fields to drop AudioClips into, or 1 field separated by commas.

    In the second bit of code your creating a new GameObject and I am guessing that "The New Guy" is a string to reference that object in the asset folder. The next line of code you are assigning with AddComponant an AudioSorce. Should that refer to the name you are looking for like <my object I'm looking for> or is AudioSource defining the type the object will be?

    Thanks a lot for the code and help.