Search Unity

What am i doing wrong

Discussion in 'Getting Started' started by witcher101, Sep 9, 2015.

  1. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    516
    I am trying to play soundclip from resources file

    private AudioSource source;
    private AudioClip cp;

    // Use this for initialization
    void Start ()

    AudioSource source = gameObject.AddComponent<AudioSource>();
    AudioClip cp = Resources.Load ("crowd") as AudioClip;

    update method
    source.PlayOneShot(cp);

    I am getting object not set to instance of object error but i have already assigned cp as audio clip
     
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Please tell us the line the error is on. Or else paste the actual error message so that we can see the line number on it, plus paste your exact code in [code ][/code] tags.

    Also, are you sure there's an audio clip called "crowd" inside a "Resources" folder? I'm not sure what happens if you don't, but perhaps the Load() command is simply returning null.
     
  3. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    516
    Its on the line
    Code (csharp):
    1.  
    2. source.PlayOneShot(cp);
    3.  
    NullReferenceException: Object reference not set to an instance of an object
    cont.Update () (at Assets/scripts/cont.cs:19)

    I do have crowd inside resources. How do i check if load is returning null
     
  4. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    immediately after loading write: Debug.Log("loaded? " + (cp != null));
     
  5. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    516
    said true
     
  6. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    So the clip is loading; the only other thing on that line which could be null is the audio source, so your problem has something to do with that.

    (more generally, notice how we're systematically going through steps? this is what you do to debug this kind of issue in the future)
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You have correctly declared properties (aka fields) called "source" and "cp". But then in your Start method, you ignore these and instead create local variables with the same names. You assign your AudioSource and AudioClip to these local variables, the Start method ends, those variables go poof, and the whole while, your properties were never touched.

    To fix this, change your Start method like so:

    Code (csharp):
    1.  
    2.         source = gameObject.AddComponent<AudioSource>();
    3.         cp = Resources.Load ("crowd") as AudioClip;
    Now, because you haven't specified a type, the compiler knows you mean to refer to the already-declared properties rather than make new local variables.
     
    jhocking likes this.
  8. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    516
    Ahh thanks it works now, didnt knew it would create duplicates
     
  9. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    doh I missed that, subtle bug