- Home /
Object Reference Error On Play Audio
I'm trying to make an audio file play but I keep getting this error and I don't know why:
error CS0120: An object reference is required to access non-static member `UnityEngine.Component.audio'
Thing is that I have another audio source that Im triggering inside an Update function and its working fine. Can I please get any pointers? Thank you.
using UnityEngine;
using System.Collections;
public class score : MonoBehaviour {
public AudioClip playScore;
static public void addPoint(){
audio.PlayOneShot (playScore);
}
}
Answer by xfelalx · Mar 20, 2014 at 04:13 PM
i think u meant gameObject.audio
If u want to access this Variable from a static method:
public AudioClip Clip; static AudioClip _clip; static AudioSource _source; static void SetAudioSource (GameObject go) { _source = go.audio; } void Awake() { _clip = Clip; SetAudioSource(gameObject); } public static void PlayStuff() { _source.PlayOneShot(_clip); }
Your answer
Follow this Question
Related Questions
For some reason Object reference not set to instance of an object 2 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Trying to get javascript to chnge UI.text but it wont 2 Answers
Error: An object reference is required to access non-static member (Enumerations) 2 Answers