- Home /
The question is answered, right answer was accepted
An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
Hello, i want to play and stop my music in my game, i don't have any problem when I do stop, but when I do play, appears this message "An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)' "
this is my code:
using UnityEngine; using System.Collections; using UnityEngine.UI;// we need this namespace in order to access UI elements within our script
public class audio : MonoBehaviour {
public bool soundoff = true;
public Button botonSoundOn;
public Button botonSoundOff;
public void start(){
botonSoundOn = botonSoundOn.GetComponent<Button> ();
botonSoundOff = botonSoundOff.GetComponent<Button> ();
}
public void PararSonido(){
Camera.main.GetComponent<AudioSource>().Stop();
soundoff = false;
}
public void PonerSonido(){
Camera.GetComponent<AudioSource>().Play();
Debug.Log ("entras");
soundoff = true;
}
}
Please help!
@lyrha Just replace the Camera.GetComponent<AudioSource>().Play();
by Camera.main.GetComponent<AudioSource>().Play();
yes, that's the solution, I solved it , I thought I had closed the subject, thanks anyway
Follow this Question
Related Questions
how to have multiple game objects play a single looping sound 2 Answers
Third person Camera With Lerp trouble 0 Answers
How to make camera position relative to a specific target. 1 Answer
How can i make sound when the gameobject exit the camera? 1 Answer
Sound OnTriggerEvent with HTC Vive Camera (head) wont play. 1 Answer